I have two tables in relation users and projects. In users table I have addiotional field admin_id
.
Pivot table is project_user
with columns: id, user_id, project_id and additonal field admin_id
When I selecting projects from database I need also to check if admin_id from pivot table
is equal
with admin_id from users table
is it possible???
Here is my User modal:
public function projects(){
return $this ->belongsToMany('App\Project','project_user')->withPivot('admin_id');
}
Here is Controller:
public function project(Project $project){
//TAKING PROJECTS FORM CURRENT ID
$projects = User::findOrFail(Auth::user() -> id) ;
//NOT IMPORTANTE
$users = User::lists('name','id');
return view('projects',array('projects' => $projects,'users' => $users ));
}
Blade:
@foreach($projects -> projects as $project)
<a href="#" class="list-group-item">{{ $project -> name }}</a>
@endforeach
I found way....
This linke:
$projects = User::findOrFail(Auth::user() -> id) ;
Should be changed with this:
$projects = User::findOrFail(Auth::user() -> id)->projects()->where('admin_id', '=', $this->id)->get();
Referring to this documentation should give you what you need: http://laravel.com/docs/5.0/eloquent#querying-relations
Specifically this section, which explains how you can add conditions to a relation query
$posts = Post::whereHas('comments', function($q)
{
$q->where('content', 'like', 'foo%');
})->get();