1

I have a problem to passing array of arrays from controller to view in Laravel. I've done some research but none of topics helped. My tables are Shops, Items, Items Price. Shops contains shop id, which I get for use from url application/id. In Items Price I got information like shop_id , item_id (these two are FK), price. This table shows which items are in which shops. And in Items I have information about items: id ,picture. When I go to application/1, I want site to show items, which are in this specific shop, information.

My controller method:

    public function getItems($id)
    {
$items=ItemPrice::where('shop_id', $id)->select('item_id')->get()->toArray();
foreach($items as $item)
 $products[] = array(Item::where('id',$item)->get()->toArray());
 $shops=Shop::all();
 return view('shop')->with(compact(['products','shops']));
   }

when I debugging array with dd($products); I get:

array:4 [▼
  0 => array:1 [▼
    0 => array:1 [▼
      0 => array:5 [▼
        "id" => 1
        "name" => "Item1"
        "price" => 0.8
        "type" => 2
        "img_dir" => "svg/d.jpg"
      ]
    ]
  ]
  1 => array:1 [▼
    0 => array:1 [▼
      0 => array:5 [▼
        "id" => 2
        "name" => "Item2"
        "price" => 1.1
        "type" => 2
        "img_dir" => "svg/d2.jpg"
      ]
    ]
  ]
  2 => array:1 [▼
    0 => array:1 [▼
      0 => array:5 [▼
        "id" => 3
        "name" => "Item3"
        "price" => 3.1
        "type" => 5
        "img_dir" => "svg/p1.jpg"
      ]
    ]
  ]
  3 => array:1 [▼
    0 => array:1 [▼
      0 => array:5 [▼
        "id" => 4
        "name" => "Item4"
        "price" => 1.56
        "type" => 5
        "img_dir" => "svg/p2.jpg"
      ]
    ]
  ]
]

In view I do foreach @foreach($products as $product) and I get error:

Trying to get property 'img_dir' of non-object.

Any help would be appreciated.

3 Answers 3

1

Try like this

public function getItems($id)
{
    $items = ItemPrice::where('shop_id', $id)
        ->select('item_id')
        ->pluck('item_id')
        ->toArray();
    $products = Item::whereIn('id', $items)->get();
    $shops = Shop::all();

    return view('shop', compact('products','shops'));
}
Sign up to request clarification or add additional context in comments.

Comments

0

You have some nested arrays in $products. What you think is a product is in fact an array. Maybe if you simplify your $products variable content :

public function getItems($id) {
    $items = ItemPrice::where('shop_id', $id)->select('item_id')->get()->toArray();
    $products = [];
    foreach($items as $item) {
        $products[] = Item::where('id',$item)->get();
    }
    $shops = Shop::all();

    return view('shop')->with(compact(['products', 'shops']));
}

1 Comment

This lead to other error: Property [img_dir] does not exist on this collection instance. And when I debugging it, collection shows just first item info.
0

Look at this line

$products[] = array(Item::where('id',$item)->get()->toArray());

$products is an array, and you assign a new element which is an array made by the array value of your query (which contains array).

So you have a 3-level nested array which leads to confusion.

Why don't you just send to your blade view $products = Item::where('id',$item)->get(); ?

1 Comment

Because in this way view display just one item info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.