【发布时间】:2020-04-26 11:10:46
【问题描述】:
我尝试获取最新的 10 产品及其图片,但只有第一张图片 这就是我尝试的方法
$newProducts = \App\Product::latest()->with(['images', function($el){
$el->first();
}])->with('category')->take(10)->get();
但它给了我这个错误
mb_strpos() expects parameter 1 to be string, object given
它在product 和image 之间具有morph 关系
产品型号
class Product extends Model {
public function images()
{
return $this->morphMany(Image::class, 'imageable');
}
}
图像模型
class Image extends Model {
public function imageable()
{
return $this->morphTo();
}
}
【问题讨论】:
标签: php laravel eloquent eloquent-relationship