(This is about Laravel 5.8)
I know you can create custom functions in your model, but I can't figure out how to make a custom function that uses data from the function morphMany.
What works:
model.php:
public function images()
{
return $this->morphMany('App\Image', 'owner');
}
page.blade.php:
@foreach($model->images() as $image)
@endforeach
This works. But I want to create a function that for example only gives the poster back. But when I place that foreach in a function inside my model. It won't loop trough the images. See the following code:
What doesn't work:
model.php:
public function images()
{
return $this->morphMany('App\Image', 'owner');
}
public function poster()
{
$images = $this->morphMany('App\Image', 'owner');
foreach($images as $image)
/* THIS CODE WILL NEVER RUN SOMEHOW */
if ($image->type == "poster")
{
return $image;
}
}
return NULL;
}
The code just returns NULL, what am I missing?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire