Facciamo un esempio, ho un controller:
Codice: Seleziona tutto
public function ClientDash($name){
$user = Auth::user()->name;
$reports = Report::with('user', 'category')->whereHas('category', function($query) use ($name) {
$query->where('title', '=', $name);
})->orderBy('date', 'desc')->paginate(15);
foreach($reports as $item){
if($item->category->title != $user){
return redirect('errors/404');
}
}
return view('dash.reports.client_dash')->with(array('reports' => $reports, 'user' => $user));
}in pratica devo prendere il "nome" della categoria del "report". Il Report è relazionato alla categoria:
Report
Codice: Seleziona tutto
public function category(){
return $this->belongsTo('App\Category');
}Codice: Seleziona tutto
public function report(){
return $this->hasMany('App\Report');
}