If you've defined the relationship in your movie model as has many then it'll be a collection. So, yes.
foreach ($actors as $actor) {
// do stuff with the current $actor
}
Have a look at the documentation: https://laravel.com/docs/5.1/eloquent-collections
ndp said:
If you've defined the relationship in your movie model as has many then it'll be a collection. So, yes.
Actors isn't a table or model, it's a column in movies table.
$movie = Product::find(1);
dd($movie->actors);
and here i get string with all actors in movie. And problem is to iterate through all actors that is delimited by comma.
$movie = Product::find(1);
$actors_array = explode(',', $movie->actors);
foreach($actors_array as $actor) {
// code
}
Or, you can save the actors as a JSON formatted string and then:
$movie = Product::find(1);
$actors = json_decode($movie->actors);
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community