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
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community