Reputation: 157
I'm struggling to filter some results.. I have a table called "Process", and another called "Actors". Process has many Actors. Here's the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Process extends Model
{
public function actors()
{
return $this->hasMany(Actor::class)->orderBy('actor');
}
}
So I have a view where I show all process and their actors. The thing is, I want to make an option to filter, where I would check if the Auth::user()->username is the same as Actor->actor.
I tried something along the lines of:
public function index()
{
$processes = Process::all();
$processes ->actors()->where('actor', 'Test')->get();
return view('process.process', compact('processes '));
}
(In the Where I compare to Test for testing purposes, if I did get it to work I would change it to Auth::user()->username obviously)
This shows the following error:
BadMethodCallException Method Illuminate\Database\Eloquent\Collection::actors does not exist.
I've tried some variations(running a foreach in the controller for example, but either I did it wrong or thats not the way to do it...) but to no avail. Any help is greatly apreciated!
Upvotes: 1
Views: 55
Reputation: 462
I assume that you want to grab all processes of the current authenticated user if yes this is what you need.
public function index()
{
$processes = Process::whereHas('actors',function($query){
$query->where('id',Auth::user()->id);
});
return view('process.process', compact('processes '));
}
Upvotes: 1