Merhaba @mgsmus
Take kullanımını sona aldım, yine de durum değişimi olmuyor.
first() firstOrFail() ile tek bir kayıt alırsam, take() işe yarıyor 1 adet kayıt dönüyor. get() kullandığımda, sadece bir adet ürüne ait bir fotoğrafı getiriyor.
İlişkiler birebir Upload modelinde
//upload modelinde
class Upload extends Model
{
// Burada yöntem adı nullableMorphs ile tanımladığımız isim ile aynı olmalı
public function photoable()
{
return $this->morphTo();
}
}
use App\Models\Category\Category;
use App\Traits\Photoable;
class Product extends Model{
use Translatable,
Photoable,
productAttribute,
SoftDeletes;
}
// photoable traits, diğer modellere yerleşiyor.
<?php
namespace App\Traits;
use App\Models\Upload\Upload;
trait Photoable
{
/**
* Modele ait tüm resimler
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function photos()
{
return $this->morphMany(Upload::class, 'photoable');
}
}
// Controller
// Products data
$products = Product::orderBy('id','DESC')->where('category_id', $category->id)->where('status', 1)->with(array('photos' => function( $query ){
$query->where('type', '=', 2)->orderBy('id', 'asc')->take(1);
}))->get();
// Blade
@foreach($products as $key => $product)
@foreach($product->photos as $photo)
<img src="{{ asset('uploads/product/'. $photo->path) }}/thumbs/270x236/{{ $photo->file }}" alt="">
@endforeach
@endforeach