简体   繁体   English

Laravel 1-N关系不起作用

[英]Laravel 1-N relationship not working

I'm starting my first Laravel project. 我正在开始我的第一个Laravel项目。 I set up and tested all the relationships between tables and all of them look fine but one 1-N relationship. 我建立并测试了表之间的所有关系,它们看起来都很好,但是只有一个1-N关系。 I cannot find the error, could you please help me? 我找不到错误,您能帮我吗?

Tables: 表格:

User: *id *username

Feeling: 感觉:

*id *name *user (it points to User:id)

Class User 班级用户

<?php

class User extends Eloquent {

    protected $table = 'User';

    public function feelings() {
        return $this->hasMany('Feeling', 'user');
    }
}

Class Feeling 课堂感觉

class Feeling extends Eloquent {

    protected $table = 'Feeling';

    public function user() {
        return $this->belongsTo('User','user');
    }
}

If I execute: 如果我执行:

$feeling = Feeling::find(1);
echo($feeling->user->username);

I get an error "Trying to get property of non-object". 我收到一个错误“试图获取非对象的属性”。

If I execute 如果我执行

$feeling = Feeling::find(1);

It prints the Json array of the feeling object and if I execute 它打印感觉对象的Json数组,如果执行

$feeling = Feeling::find(1);
echo($feeling->user);

It prints "1". 打印“ 1”。 So I'm getting the user ID of the database, but not the user object. 因此,我正在获取数据库的用户ID,而不是用户对象。 I would like to get the user object instead. 我想改为获取用户对象。

Thank you very much. 非常感谢你。

Problem: 问题:

Everything is fine but you have used user as your local key and that's why the confusion because, when you call $feeling->user the magic method __get() is called which is available in the Illuminate/Database/Eloquent and it; 一切都很好,但是您已经将user用作本地密钥,这就是造成混乱的原因,因为当您调用$feeling->user时,会调用魔术方法__get() ,该方法在Illuminate/Database/Eloquent __get()中可用,并且它可用; as given below: 如下所示:

public function __get($key)
{
    return $this->getAttribute($key);
}

The getAttribute() method looks several places for the key and sequentially it looks into the Model itself at first and then looks into the relationship array and then at last checks if the key for examaple in your case user exists as a method in the Model which is Feeling in your case, then it gets the related model and finds the key value. getAttribute()方法在几个地方寻找关键字,然后依次检查Model本身,然后查看关系数组,然后最后检查案例user中的示例关键字是否作为Model的方法存在,是Feeling在你的情况,那么它得到的相关模型,并发现该键值。

Hence, your Feeling has a field as user so before it goes to the related model it just finds the field in current Model 's attributes so it thinks that, user field has been requested for and you get the numeric id instead if related model instance. 因此,您的Feeling有一个作为user的字段,因此在进入相关模型之前,它只是在当前Model的属性中找到该字段,因此它认为已请求了user字段,而您获得了数字id而不是相关模型实例。

Solution: 解:

Solution is pretty simple, change the related field name ( user ) to anything else, better is user_id but you are free to use anything but make sure that doesn't create any further confusions. 解决方案非常简单,将相关字段名称( user )更改为其他名称,最好是user_id但是您可以随意使用任何名称,但要确保不会造成任何进一步的混乱。 So, if you change it to user_id then make changes to your both models, for example: 因此,如果将其更改为user_id则对两个模型都进行更改,例如:

public function user() {
    return $this->belongsTo('User','user_id');
}

But, if you use user_id then you may omit the second argument from both model's relationship method, so you may write: 但是,如果您使用user_id则可以从两个模型的Relationship方法中省略第二个参数,因此您可以编写:

// Feeling
return $this->belongsTo('User');

// User
return $this->hasMany('Feeling');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM