Talib Hussain
Talib Hussain

Reputation: 1344

add new element in laravel collection object

I want to add new element in $items array, I don't want to use joins for certain reasons.

$items = DB::select(DB::raw('SELECT * FROM items WHERE items.id = '.$id.'  ;'));
foreach($items as $item){
    $product = DB::select(DB::raw(' select * from product
           where product_id = '. $id.';' ));

    $item->push($product);
}

What should I do?

Upvotes: 95

Views: 310617

Answers (7)

aboo
aboo

Reputation: 99

$item = collect();
$item->push($product);

Upvotes: -1

Austin Grant
Austin Grant

Reputation: 11

This is what i would do...

$items = Item::find($id);
foreach($items as $item){
    $product = Product::find($id);
    $item->product = $product;
}

This would assign $product to each $item

Upvotes: 1

Pastor Bones
Pastor Bones

Reputation: 7351

It looks like you have everything correct according to Laravel docs, but you have a typo

$item->push($product);

Should be

$items->push($product);

push method appends an item to the end of the collection:

I also want to think the actual method you're looking for is put

$items->put('products', $product);

put method sets the given key and value in the collection

Upvotes: 190

Alpy
Alpy

Reputation: 789

As mentioned above if you wish to add as a new element your queried collection you can use:

    $items = DB::select(DB::raw('SELECT * FROM items WHERE items.id = '.$id.'  ;'));
    foreach($items as $item){
        $product = DB::select(DB::raw(' select * from product
               where product_id = '. $id.';' ));

        $items->push($product);
        // or 
        // $items->put('products', $product);
    }

but if you wish to add new element to each queried element you need to do like:

    $items = DB::select(DB::raw('SELECT * FROM items WHERE items.id = '.$id.'  ;'));
    foreach($items as $item){
           $product = DB::select(DB::raw(' select * from product
                 where product_id = '. $id.';' ));
    
          $item->add_whatever_element_you_want = $product;
    }

add_whatever_element_you_want can be whatever you wish that your element is named (like product for example).

Upvotes: 18

sinan aydın
sinan aydın

Reputation: 49

If you want to add a product into the array you can use:

$item['product'] = $product;

Upvotes: 3

JSowa
JSowa

Reputation: 10572

If you want to add item to the beginning of the collection you can use prepend:

$item->prepend($product, 'key');

Upvotes: 13

Ticherhaz FreePalestine
Ticherhaz FreePalestine

Reputation: 2377

I have solved this if you are using array called for 2 tables. Example you have, $tableA['yellow'] and $tableA['blue'] . You are getting these 2 values and you want to add another element inside them to separate them by their type.

foreach ($tableA['yellow'] as $value) {
    $value->type = 'YELLOW';  //you are adding new element named 'type'
}

foreach ($tableA['blue'] as $value) {
    $value->type = 'BLUE';  //you are adding new element named 'type'
}

So, both of the tables value will have new element called type.

Upvotes: 1

Related Questions