Laravel: belongsToMany() does not get fields in the many-to-many table Laravel: belongsToMany() does not get fields in the many-to-many table laravel laravel

Laravel: belongsToMany() does not get fields in the many-to-many table


If you refer to the laravel docs on pivot tables, you will need to add withPivot on your relationships.

In your example you will need to add the following:

class User extends Eloquent {    public function clients()    {        return $this->belongsToMany('Client', 'user_client')->withPivot('rate');    }}

Update your view like:

<h1>{{$user->name}}</h1>@foreach($user->clients as $client)    <p>{{$client->name}}, {{$client->pivot->rate}}</p>@endforeach

I would also eager load the clients to save you time:

public function show($username) // foo.com/user/{$username}{    $user = User::with('clients')->where('username', '=', $username)->firstOrFail();    return View::make('users.show', compact('user'));}

Hope that helps :)