Support the ongoing development of Laravel.io →
Database Views Blade
Last updated 2 years ago.
0

If you've defined the relationship in your movie model as has many then it'll be a collection. So, yes.

0
foreach ($actors as $actor) {
    // do stuff with the current $actor
}

Have a look at the documentation: https://laravel.com/docs/5.1/eloquent-collections

0

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.

0
$movie = Product::find(1);
$actors_array = explode(',', $movie->actors);

foreach($actors_array as $actor) {
    // code
}

PHP explode.

Or, you can save the actors as a JSON formatted string and then:

$movie = Product::find(1);
$actors = json_decode($movie->actors);

PHP json_decode.

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.