How to filter output that chain ( with and where ) function. Bottom output is wwithout chain with where chain. cause it return error if chain with where.Bottom ouput return 2 person with different job. i need to filter the job by Artist. each model person, job and media using relationship belongsToMany. So there're 2 junction table.
[
{
"person_id": 1,
"first_name": "suzuki",
"last_name": "amanda"
}
][]
Tables
media
people
media_people
jobs
person_jobs
Controller
$comics = Media::where('type_id', 3)->get();
foreach( $comics as $comic )
{
$artists = $comic->people()->whereHas('jobs', function( $query ){
$query->where('job_name', '=', 'Artist');
})->get();
//json output above without index access
echo json_encode($artists, JSON_PRETTY_PRINT);
// but as output result like 2 array. the last one is empty. i try
access with index. It return suzuki with error undefined array index
0
echo json_encode($artists[0]->first_name, JSON_PRETTY_PRINT);
}