1
这是我产品型号:()方法返回所有的模型实例,而不是一个实例中laravel
class Product extends Model
{
protected $primaryKey = 'product_id';
public $guarded = ['created_at', 'updated_at'];
public function product_pics()
{
return $this->hasMany('App\ProductPics');
}
}
这是ProductPics型号:
class ProductPics extends Model
{
public $timestamps = false;
protected $fillable = ['pic_name'];
protected $primaryKey = 'pic_id';
public function product()
{
return $this->belongsTo('App\Product');
}
}
现在我想要在ProductController show()方法中获取特定产品及其所有产品图片。对于我这样写:
public function show ($id)
{
$product = Product::find($id)->with('product_pics')->get();
return $product;
return view('main.pages.product')->with(['product'=> $product]);
}
但事与愿违,而我用find()方法只选择一个模式,它返回一组与相关产品的图片全部产品模型。
什么是问题?
是的,我试试这个已经和作品,但它不被接受逻辑上对我来说。但是不应该先找到模型然后获取ProductPics? –