Итак, у меня есть изображения, которые сохраняются конкретной моделью каждый раз, когда они записываются (например, поток, ответ, комментарий).
Я хочу вызвать, чтобы иметь возможность вызывать эти значения, чтобы получить путь, откуда берутся изображения.
Пока у меня есть этот код:
В каждой из моделей (Тема, Ответ, Комментарий):
public function images()
{
return $this->morphMany(Image::class, 'imageable');
}
В модели изображения:
public function image()
{
return $this->morphTo();
}
В представлении (я не могу понять, как здесь вызвать связанную модель)
<div class = "image-show">
Found here:
@if ($image->imageable_type == 'App\Thread')
<a href = "/forums/{{ $image->image->channel->slug }}/{{ $image->image->slug }}/">
{{ $image->image->title }}
</a>
@endif
@if ($image->imageable_type == 'App\Reply')
<a href = "">
{{ dd($image->image()->path()) }}
</a>
@endif
@if ($image->imageable_type == 'App\ProfilePost')
<a href = "{{ $image->path() }}">{{ $image->image->user->name }}'s profile
</a>
@endif
@if ($image->imageable_type == 'App\ProfilePostComment')
@endif
@if ($image->imageable_type == 'App\Product')
@endif
@if ($image->imageable_type == 'App\Review')
@endif
@if ($image->imageable_type == 'App\ReviewComment')
@endif
@if ($image->imageable_type == 'App\Thread')
@endif
@if ($image->imageable_type == 'App\ProductComment')
@endif
<img src = "{{ $image->path }}">
</div>
Может ли кто-нибудь помочь показать мне, как я могу вызвать отношение imageable_type, чтобы получить атрибуты моделей и использовать их соответствующим образом? Спасибо!






Следуя соглашениям, определение отношения в модели изображения должно быть
//-----------------------------Option 1------------------------
public function imageable()
{
return $this->morphTo();
}
//Access the related like
$image->imageable
//-----------------------------Option 2------------------------
//OR if you want to keep it as image, specify type & id columns
public function image()
{
return $this->morphTo('image', 'imageable_type', 'imageable_id');
}
//Access the related like
$image->image