Laravel模型之间的关系不起作用

发布于 2025-02-13 17:52:57 字数 503 浏览 1 评论 0原文

我有2种型号:一种用于用户的模型,一种用于客户。用户是客户

用户具有“ codigocli”字段,客户端具有“ codigo”字段,

我的模型之间的关系就是这样:

//User model
public function cliente()
{
    return $this->hasOne(Cliente::class, 'codigo', 'codigocli');
}


//Cliente model
public function user()
{
    return $this->belongsTo(User::class, 'codigocli', 'codigo');
}

我的数据库很好(我认为)客户端具有“ codigo”字段,用户具有'codigocli ' 场地。那我做错了什么?当我想用dd(auth() - > user() - clible())查询我的home.blade.php时,我什么都没有得到任何东西,尽管它向我显示了父对象罚款。

I have 2 models: one for users and one for clients. A user is a customer

User has a 'codigocli' field and client has a 'codigo' field

The relationships between my models are like this:

//User model
public function cliente()
{
    return $this->hasOne(Cliente::class, 'codigo', 'codigocli');
}


//Cliente model
public function user()
{
    return $this->belongsTo(User::class, 'codigocli', 'codigo');
}

My database is fine (I think) client has the 'codigo' field and users has the 'codigocli' field. So what am I doing wrong? When I want to query my home.blade.php with dd(auth()->user()-cliente()) I don't get anything, although it shows me the parent object fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凡尘雨 2025-02-20 17:52:58

h
您在这里有一个OneToone关系,因此,如果您不更改ID名称,请尝试使用:

//User model
public function cliente()
{
    return $this->hasOne(Cliente::class, 'codigocli');
}
//Cliente model
public function user()
{
    return $this->belongsTo(User::class, 'codigocli');
}

如果您更改ID名称:

//User model
public function cliente()
{
    return $this->hasOne(Cliente::class, 'codigocli','local_id_name' );
}
//Cliente model
public function user()
{
    return $this->belongsTo(User::class, 'codigocli', 'local_id_name');
}

H
You have an OneToOne relationship here so try this if you don't change id name:

//User model
public function cliente()
{
    return $this->hasOne(Cliente::class, 'codigocli');
}
//Cliente model
public function user()
{
    return $this->belongsTo(User::class, 'codigocli');
}

if you change id name:

//User model
public function cliente()
{
    return $this->hasOne(Cliente::class, 'codigocli','local_id_name' );
}
//Cliente model
public function user()
{
    return $this->belongsTo(User::class, 'codigocli', 'local_id_name');
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文