Well you return a new Wedding, since you don't pass any model to the constructor of your repository. This means that you work with a new Wedding model. So this model is not associated with anything since it's not even saved in the database.
Jan 28, 2016
2
Level 1
BelongToMany With Pivot
Hi I hope this makes sense. What i want to do is gather a list of users whose role is guest i.e. has a role id of 3. Ive created a ManytoMany relationship between users and weddings tables and the pivot table includes the role_id column. I cant seem to be able to return a list of users names no matter what I do! Can some help? Below is what I have done so far.
Wedding Model
public function accounts()
{
return $this->belongsToMany(Account::class, 'account_weddings', 'wedding_id', 'account_id')->withPivot('role_id');
}
Account Model
public function weddings()
{
return $this->belongsToMany(Wedding::class, 'account_weddings', 'account_id', 'wedding_id')->withPivot('role_id');
}
Wedding Repository
public function __construct($model = null)
{
$this->model = $model ?: new Wedding;
}
public function retrieveGuestList()
{
$this->model = $this->model->accounts()->where('role_id' , '3')->get();
return $this;
}
Wedding controller
public function guestlist()
{
$wedding = new WeddingRepository();
return view()->make('wedding.guestlist')->with('weddings', $wedding->retrieveGuestList());
}
Please or to participate in this conversation.