Hi ItsRD , assuming that you have owner as a boolean in your migration here is what you have to do.
public function users()
{
return $this->belongsToMany('App\User')->withPivot('owner');
}
withPivot lets you define others columns apart from the ids of the relationship. Documentation in the section: Retrieving Intermediate Table Columns
public function projects()
{
return $this->belongsToMany('App\Project')->withPivot('owner');
}
The same here.
Route::get('attach',function (){
$user = App\User::find(1);
$user->projects()->attach(1,['owner'=> true]);
$user->projects()->attach(3,['owner'=> false]);
$user = App\User::find(2);
$user->projects()->attach(3,['owner'=> true]);
});
I just create a database seeder with three projects and two users and use the route for attaching. Do it as you like it.
<h1>Name: {{ $project->name }}</h1>
<p>Users: </p>
@foreach($project->users as $user)
<li>{{$user->name}} Owner: {{ $user->pivot->owner }}</li>
@endforeach
Out of topic: Where are you from?
Awesome, it worked! Thank you so much!
I'm from the Netherlands and yea... I know my english is terrible.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community