Tôi đang cố gắng sử dụng Eloquent để có được một sản phẩm cụ thể có cột brand_id
ánh xạ tới một bảng brands
, mảng brand
sẽ trở lại trống.Laravel Eloquent với() -> trả về null
Có điều gì rõ ràng ở đây cần được thay đổi không?
$product = Product::with('images')->with('brand')->select($fields)->where('display', '=', 1)->find($id);
// mô hình sản phẩm
class Product extends Eloquent {
...
public function brand()
{
return $this->belongsTo('Brand');
}
// Nhãn hiệu mô hình
class Brand extends Eloquent {
...
public function products()
{
return $this->hasMany('Product');
}
have You checked it with: $ brand = $ product-> brand(); ? – num8er
Trường nào trong biến '$ fields'? –