Laravel5.8 belongsTo 关系不适用于 foreach 循环
Laravel5.8 belongsTo relation not working for foreach loop
我正在使用 hasMany 和 belongsTo 处理 Laravel 关系。它适用于 hasMany
关系,但我在 belongsTo
for collection foreach 循环中遇到问题。这是我的 categories
table.
id name
--------------
1 food
和products
table.
id name category_id
----------------------------------
1 pizza 1
2 hamburger 1
以下是hasMany
产品型号。
# Product.php
public function products()
{
return $this->hasMany(Product::class);
}
下面是belongsTo
类目模型。
# Category.php
public function category()
{
return $this->belongsTo(Category::class, 'id');
}
我在 blade 中遇到错误:
Trying to get property of non-object
<tbody>
@foreach ($products as $product)
<tr>
<td> {{ $product->category->name }} </td>
</tr>
@endforeach
</tbody>
如有任何建议或指导,我们将不胜感激,谢谢
问题出在方法定义上。 belongsTo
方法的第二个参数是外键列名。试试这个:
# Product.php
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
但是,鉴于您的外键是型号名称后跟 _id
(category_id
),Laravel 默认会查找此键..所以您可以简化它像这样:
# Product.php
public function category()
{
return $this->belongsTo(Category::class);
}
... In the example above, Eloquent will try to match the post_id
from the Comment
model to an id
on the Post
model. Eloquent
determines the default foreign key name by examining the name of the
relationship method and suffixing the method name with a _
followed
by the name of the primary key column. However, if the foreign key on
the Comment
model is not post_id
, you may pass a custom key name
as the second argument to the belongsTo
method:
/**
* Get the post that owns the comment.
*/
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key');
}
Category.php
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
而不是 'id' in category() 使用 'category_id'..... 因为你已经给 category_id 作为外键....所以 laravel 会搜索它...
我正在使用 hasMany 和 belongsTo 处理 Laravel 关系。它适用于 hasMany
关系,但我在 belongsTo
for collection foreach 循环中遇到问题。这是我的 categories
table.
id name
--------------
1 food
和products
table.
id name category_id
----------------------------------
1 pizza 1
2 hamburger 1
以下是hasMany
产品型号。
# Product.php
public function products()
{
return $this->hasMany(Product::class);
}
下面是belongsTo
类目模型。
# Category.php
public function category()
{
return $this->belongsTo(Category::class, 'id');
}
我在 blade 中遇到错误:
Trying to get property of non-object
<tbody>
@foreach ($products as $product)
<tr>
<td> {{ $product->category->name }} </td>
</tr>
@endforeach
</tbody>
如有任何建议或指导,我们将不胜感激,谢谢
问题出在方法定义上。 belongsTo
方法的第二个参数是外键列名。试试这个:
# Product.php
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
但是,鉴于您的外键是型号名称后跟 _id
(category_id
),Laravel 默认会查找此键..所以您可以简化它像这样:
# Product.php
public function category()
{
return $this->belongsTo(Category::class);
}
... In the example above, Eloquent will try to match the
post_id
from theComment
model to anid
on thePost
model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with a_
followed by the name of the primary key column. However, if the foreign key on theComment
model is notpost_id
, you may pass a custom key name as the second argument to thebelongsTo
method:/** * Get the post that owns the comment. */ public function post() { return $this->belongsTo('App\Post', 'foreign_key'); }
Category.php
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
而不是 'id' in category() 使用 'category_id'..... 因为你已经给 category_id 作为外键....所以 laravel 会搜索它...