Skip to main content

Laravel belongsToMany not working as expected

i have an issue getting user related data from my belongsToMany relationship where i need to get all data from the Review class with all the user data from the pivot table.

Reviews model:

public function user() {
   return $this->belongsToMany(User::class, 'reviews_pivot', 'review_id', 'user_id');
}

UserController:

public function edit($id) {
  $reviews = Reviews::with('user')->get();
}

View:

@foreach($reviews as $review)
  <input type="text" name="" value="" />
@endforeach

Now when i look for user id 1, where the pivot table is empty it shows the data from user id 2 and so on.



source https://stackoverflow.com/questions/68581415/laravel-belongstomany-not-working-as-expected

Comments