Laravel Eloquent属于关系返回null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47730514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent belongsto relationship returns null
提问by haisom
I have two Eloquent models:
我有两个 Eloquent 模型:
1) Post
1) 发布
class Post extends Model
{
protected $table = 'posts';
protected $fillable = ['id', 'user_id', 'product_id', 'site_id', 'link_id', 'body', 'created_at', 'updated_at'];
public function user(){
return $this->belongsTo(User::class);
}
public function product(){
return $this->belongsTo(Product::class);
}
2) Product
2) 产品
protected $table = 'products';
protected $fillable = ['id', 'user_id', 'manufacturer_id', 'shift_product_id', 'name', 'english_name',
'slug', 'text', 'spec', 'live', 'created_at', 'updated_at'];
public function posts(){
return $this->hasMany(Post::class);
}
I need to get the product from a post I do that:
我需要从我这样做的帖子中获取产品:
$posts = Post::get();
foreach($posts as $key){
dd($key->product);
}
Like this it returns NULL
If I do like this:
dd($key->product());
I get the product but I can't to use that
像这样它返回 NULL 如果我这样做:
dd($key->product());
我得到了产品但我不能使用它
but I need to get something like that to use whant I need:
但我需要得到类似的东西来使用我需要的东西:
回答by Adam Kozlowski
Try to point out foregin key and other key in relation, examples:
尝试指出foregin key和other key的关系,例子:
public function post()
{
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
}
public function user()
{
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}
回答by lagbox
The relationship probably doesn't exist in the database.
该关系可能不存在于数据库中。
Based on your fillable
array on Post
, the way you have the relationships setup looks correct as you are following naming conventions for keys and your belongsTo
relationship methods have the correct name for convention.
根据您的fillable
数组Post
,您设置关系的方式看起来是正确的,因为您遵循键的命名约定,并且您的belongsTo
关系方法具有正确的约定名称。
$post->product()
is not returning your Product
model. It is returning a Relation
type object (BelongsTo
). This is used for querying the relationship. $post->product
would be the dynamic property for the relationship that would return the already loaded relationship or load the relationship and give you the result.
$post->product()
没有返回您的Product
模型。它返回一个Relation
类型对象 ( BelongsTo
)。这用于查询关系。$post->product
将是关系的动态属性,它将返回已加载的关系或加载关系并为您提供结果。
Laravel 5.5 Docs - Eloquent - Relationships - Relationship Methods Vs. Dynamic Properties
Laravel 5.5 文档 - Eloquent - 关系 - 关系方法 Vs。动态属性
If the relationships are setup correctly $post->product
being null
would mean the relationship doesn't actually exist in the database, no matching id
in products
for product_id
or product_id
being null. (assuming no foreign key constraint)
如果关系是正确设置$post->product
是null
将意味着关系实际上并不存在于数据库中,没有匹配id
的products
为product_id
或product_id
为空。(假设没有外键约束)
Side note: eager loading the relationship would be a good idea:
旁注:急切加载关系将是一个好主意:
$posts = Post::with('product')->get();
回答by haisom
i found my problem i dont have in the DB product with ID = 1 :/ stuped problem
我发现我的问题在 ID = 1 的 DB 产品中没有:/ stuped 问题
thanks for all the help i leran alot from u.
感谢您对我的帮助。
回答by truffolone
I just came across this post because I got a similar error while working on a project.
我刚刚看到这篇文章是因为我在做一个项目时遇到了类似的错误。
What I discovered is that when you query a model with the all() method, it ignores the related softdeleted rows.
我发现当您使用 all() 方法查询模型时,它会忽略相关的软删除行。
When you try to access them tho, you get the null
当您尝试访问它们时,您会得到 null
回答by digout
Remember to hit save() after associate and dissociate. Got me a couple of times:
记得在关联和分离后点击 save() 。找过我几次:
$model->relation()->associate($record)->save();