1

I asked question whose link is: Link

Now my problem is that I want to show "CategoryName' of in food details.For that I add function in Categories.php model as:

 public function food()
{
return $this->hasMany('App\Food','food_categories','Category_id','Food_id');
}

and in food.php

public function restaurant()
{
    return $this->belongsToMany('App\Restaurant','food_restaurant','Food_id','Res_id');       
}

public function categories()
{
    return $this->belongsTo('App\Categories','food_categories');
}

Then in show.blade.php I add:

@foreach ($food->restaurant as $restaurant) 

<h3><p>RestaurantName:</h3><h4>{{$restaurant->ResName}}</p></h4>
<h3><p>Contact #:</h3><h4>{{$restaurant->Contact}}</p></h4>
<h3><p>Location:</h3><h4>{{$restaurant->Address_Loc}}</p></h4>

@endforeach 


@foreach ($food->categories as $categories) 

<h3><p>CategoryName:</h3><h4>{{$categories->CategoryName}}</p></h4>

@endforeach

And I changed controller to :

public function show($Food_id)
{
$food = Food::with('restaurant.categories')->findOrFail($Food_id);
return view('show', compact('food'));
}

But it does not shows me categoryname.Plz help me where is the problem?

8
  • Do you have the CategoryName column in Categories table? Commented Aug 16, 2016 at 12:34
  • Yes I have categories table having "Categoryid,CategoryName" columns. Commented Aug 16, 2016 at 12:38
  • Should't it be $food->restaurant ? and a restaurant having categories? (Checked with the previous question) I can't see the Food model having categories... Commented Aug 16, 2016 at 12:41
  • I added categories in food model as shown above in food.php Commented Aug 16, 2016 at 12:45
  • My bad, have you tried @foreach ($food->categories() as $categories) ? Like calling the method? Commented Aug 16, 2016 at 12:47

1 Answer 1

2

With Eager loading, "dot" notation loads nested relations. In your controller you do

Food::with('restaurant.categories')

..this queries the restaurant() relation on the Food model, and that Restaurant's categories() relation. I think you might need to call

Food::with('restaurant', 'categories')

as this will query both relations on the Food model.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried before this one.Can we use foreach same way as shown in show.blade.php?it gives me error "Invalid argument supplied for foreach()" in show.blade.php
It could be that the 'categories' property is not visible for your Model. Try dd($food->categories); in your Controller, right before your return statement and see what it returns.
So there's your problem. Check if: 1. your Food model has correct settings for $visible or $hidden property 2. your Food model's table has a food_categories column that contains the foreign key (I think this might be the problem, this seems like a weird column name to me) 3. the foreign keys are set up correctly in your database/migrations

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.