Support the ongoing development of Laravel.io →
Database
Last updated 2 years ago.
0

you just set the relationship between user and restaurant table and get the join data,

when user post a review each restaurant at this time you store the user id in review table

0

Could you post your model relationships?

0

Taylor provides some great documentation on this matter.

Let's say you have three tables, 'restaurants', 'reviews', and 'users'. The 'restaurants' table would have things like a id, title, description, etc. Your 'reviews' table would have things like id, score (e.g. A number between 1 and 5), title, body, and restaurant_id which is an integer value that matches the id column of the 'restaurants' table. You'd also have another column which is also an integer that matches the user, so it would be 'user_id'. Last, you have your 'users' table with id, name, email, etc.

Now in your Restaurant model you'd need something like this:

public function reviews()
{
    return $this->hasMany(Reviews::class);
}

Then in your Review model you'd need these:

public function restaurant()
{
    return $this->belongsTo(Restaurant::class);
}

public function user()
{
    return $this->belongsTo(User::class);
}

Lastly, in your User model:

public function reviews()
{
    return $this->hasMany(Review::class);
}

Now referencing your controller:

$restaurant = Restaurant::with('reviews.user')->find($restaurantId);

And in your blade file:

@foreach ($restaurant->reviews as $review)
    <p>{{ $review->user->name}} gave this a score of {{ $review->score }}</p>
@endforeach

Hope that helps.

Last updated 7 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.