jeudi 28 février 2019

Laravel collection issue

after a lot googeling and searching through stackoverflow i decided i need your help.

I have to get the quantity of supplies for each supply by a certain supplier. I know that sounds weird but let me explain:

I have a Supplier model wich has many supplies and each supply has many stocks wich have a quantity. So now im building a view in wich i want to represent each Supply with their quantity on a certain supplier.

So thats what I came up with in my controller:

foreach ($suppliers as $supplier) {

        foreach($supplier->supplies as $supply){

            if(!$comp_supplies->contains('name', $supply->name)){

                $comp_supplies->push(['name'=>$supply->name, 'supplier'=>[['name'=> $supplier->name, 'quantity' => $supply->stocks->first()->quantity]]]);

            }elseif($comp_supplies->contains('name', $supply->name)){

                $array = (['name'=> $supplier->name, 'quantity' => $supply->stocks->first()->quantity]);
                $array2 = $comp_supplies->where('name', $supply->name)->first()['supplier'];

                array_push($array2, $array);

                //dd($array2);
                $comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2;
                dd($comp_supplies->where('name', $supply->name)->first()['supplier']);
            }
        }

    }

So im iterating over my suppliers and iterate again over the supplies from each of them. Now I want to fill the collection I want as a result.

If this collection not contains a supply with the name of "$supply->name", I push an array with the supply-name and create an array "suppliers" in which I also set the first entry with the current supplier information.

Now we are getting close to my problem.

If comp_supply already contains a supply with the current supply-name, if have to push the new supplier into the already existing "supplier" array we created in the first "if".

Therefore I created $array, which holds the new supplier-informations, and $array2, which holds the supplier-array ($comp_supplies->where('name', $supply->name)->first()['supplier']) we already made.

Now, if i push $array onto $array2 and dd(array2) everything works as i want it to. But if I now set

$comp_supplies->where('name', $supply->name)->first()['supplier'] = $array2 

and then

dd($comp_supplies->where('name', $supply->name)->first()['supplier']);

it didnt change.

Im stuck on this Problem for nearly 2h and getting really frustrated.

Please if anyone has an idea what I could do to solve this, or knows where i can look next, let me know.

Here are also the migrations:

Supplier:

public function up()
{
    Schema::create('suppliers', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->unique();
        $table->unsignedInteger('coordinates_id')->index()->nullable();
        $table->timestamps();
    });
}

Supplies:

public function up()
{
    Schema::create('supplies', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->unsignedInteger('supplier_id');
        $table->foreign('supplier_id')
              ->references('id')
              ->on('suppliers')
              ->onDelete('cascade');
        $table->timestamps();
    });
}

Stocks:

    public function up()
{
    Schema::create('stocks', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('supplies_id');
        $table->foreign('supplies_id')
              ->references('id')
              ->on('supplies')
              ->onDelete('cascade');
        $table->integer('quantity');
        $table->timestamps();
    });
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire