1
votes

I am developing an online ecommerce for the first time and currently, i am not able to iterate through my collection.

Every item for a shop is categorized into a product category. My relationship is below

Category

public function items()
  {
     return $this->belongsToMany('App\Item','category_item','category_id','item_id')
            ->withTimestamps();
  }

Items

public function categories()
 {
    return $this->belongsToMany('App\Category','category_item','item_id','category_id')
        ->withTimestamps();
 }

This code here is able to fetch the groups and their products. I try to loop through to get the names of the products like below but it only displays the name of the last product in the database.

Why is this happening?

ItemController

//get id of product categories and display all products in grid table

$items_in_table = Category::whereIn('id',$request->get('product_category'))->with('products')->get();
foreach($items_in_table as $item)
 {
   return $item->name;
 }

update

 $temp
  foreach($items_in_table as $item)
  {
   temp = $item;
  }

return $temp

response

{"id":2,"title":"freight","no_of_contacts":0,"user_id":1,"created_at":"2018-04-15 23:55:30","updated_at":"2018-04-15 23:55:30","items":[{"id":1,"name":"AirBag ","phone":"0247878234","group_id":null,"user_id":1,"created_at":"2018-04-16 00:14:20","updated_at":"2018-04-16 05:31:05","pivot":{"group_id":2,"customer_id":1,"created_at":"2018-04-16 05:33:08","updated_at":"2018-04-16 05:33:08"}}

enter image description here

3
You must return a view with the view name you wish to render and which variables should the view use, smth like: return view('products.list',[products=>$items_in_table]); Do you need a JSON response or a rendered template response because from your code there is no way to tell, furthermore it breaks at the first item because of the return statement which is expected - ka_lin
@ka_lin, i don't need a rendered template response. I need to fetch the names of the items displayed from each group - user9653376
Make a temp variable and append to it in the foreach and return the temp variable - ka_lin
@ka_lin please look at my update and my json response. I have higlighted item in there.. I am trying to reach that and display the name - user9653376
Deleted my answer, basically it's what Amr Aly said ... ish..., it is not clear what you are trying to achieve:1. You want to return a json 2.You stated "and display all products in grid table"...you need to send $items_in_table to a view and make 2 foreach to render them - ka_lin

3 Answers

0
votes

Maybe you need to put it in an array and then rotate it

$items_in_table = Category::whereIn('id',$request->get('product_category'))->with('products')->get();

$name = [];

foreach($items_in_table as $item)
{
  $name[] = $item->name;
}

return $name;
0
votes

foreach will not manipulate the values of the array

Some possible solutions:

  1. Use a reference, this will change the values in $items_in_table

    foreach($items_in_table as &$item) { $item = $item->name; }

  2. Use map (since it is an Laravel-collection), this will also alter the array

    $items_in_table->map(function ($item) { return $item->name; })

0
votes

let's define our relationships first

Category:

public function items()
  {
     return $this->belongsToMany('App\Item')
            ->withTimestamps();
  }

Item:

public function categories()
 {
    return $this->belongsToMany('App\Category')
        ->withTimestamps();
 }

then in your controller:

$items_in_table = Category::with('items')->get();
$names = [];
foreach($items_in_table as $category) {
  foreach($category->items as $item) {
   $names[] = $item->name;
  }
}

dd($names);