Laravel 5.1 - BelongsTo 关系返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34459225/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:53:20  来源:igfitidea点击:

Laravel 5.1 - BelongsTo relationship returns null

phpooplaraveleloquentrelationship

提问by Felipe Francisco

\App\User

\应用\用户

class User

public function status() {

    return $this->belongsTo('App\UserStatus', 'user_status_id', 'id');
}

\App\UserStatus

\应用程序\用户状态

class UserStatus

protected $fillable = ['id'];

public function user() {

    return $this->hasMany('App\User', 'user_status_id', 'id');
}

I already have the $userobject from a simple User::find()query with some fields, then I try to access the statusobject by lazy loading it with $user->load('status')method.

我已经$userUser::find()带有一些字段的简单查询中获得了status对象,然后我尝试通过使用$user->load('status')方法延迟加载来访问该对象。

I'm following the docs, but it seems useless since $user->statusstill returns null.

我正在关注文档,但它似乎没用,因为它$user->status仍然返回 null。

public function foo(User $user) {

    $user->load('status');
    $user->status // returns null
}

What am I doing wrong?

我究竟做错了什么?

--------- SOLUTION ---------

- - - - - 解决方案 - - - - -

Actually, to lazy load any relationship, the foreign key value needs to be stored in the model object.

实际上,要延迟加载任何关系,需要将外键值存储在模型对象中。

In my findoperation, I wasn't querying the user_status_idfield. When I added this field into the query, the $user->statusstatement started to return the UserStatusmodel.

在我的find操作中,我没有查询该user_status_id字段。当我将此字段添加到查询中时,$user->status语句开始返回UserStatus模型。

I don't think this information is written on the Laravel docs, it may be simple, but it took me some time to figure that out.

我不认为这些信息是写在 Laravel 文档上的,它可能很简单,但我花了一些时间才弄明白。

回答by Felipe Francisco

Actually, to lazy load any relationship, the foreign key value needs to be stored in the model object.

实际上,要延迟加载任何关系,需要将外键值存储在模型对象中。

In my find operation, I wasn't querying the user_status_id field. When I added this field into the query, the $user->status statement started to return the UserStatus model.

在我的查找操作中,我没有查询 user_status_id 字段。当我将此字段添加到查询中时,$user->status 语句开始返回 UserStatus 模型。

I don't think this information is written on the Laravel docs, it may be simple, but it took me some time to figure that out.

我不认为这些信息是写在 Laravel 文档上的,它可能很简单,但我花了一些时间才弄明白。

回答by Vojko

in status() relation replace the line with

在 status() 关系中,用

return $this->belongsTo('\App\UserStatus', 'user_status_id');

in user() relation with this

在 user() 与 this 的关系中

return $this->hasMany('\App\User', 'user_status_id');

Long story short add a '\' before App and remove third parameter since it is not many-to-many relationship.

长话短说在 App 之前添加一个 '\' 并删除第三个参数,因为它不是多对多关系。

Also make sure that you actually use Eloquent so add on top of models

还要确保你真的使用了 Eloquent 所以添加到模型之上

namespace App;

use Illuminate\Database\Eloquent\Model;

class MODELNAME extends Model

and assign a table

并分配一张桌子

protected $table = 'model_table';

回答by Brandon14

I was using Psy Shell to run it. For the first argument in belongsTo(), that could just be Classname::class. I encountered the case that I cannot perform belongsTo(), but if you restart your Psy Shell, it worked. It is weird though.

我正在使用 Psy Shell 来运行它。对于belongsTo() 中的第一个参数,它可能只是Classname::class。我遇到了无法执行belongsTo()的情况,但是如果您重新启动Psy Shell,它就起作用了。虽然很奇怪。