Laravel属于抛出未定义的错误 [英] Laravel belongs to throwing undefined error

查看:204
本文介绍了Laravel属于抛出未定义的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个模型.

用户:

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'users';

    public function group () 
    {
        return $this->belongsTo('Group');
    }
}

并分组:

class Group extends Eloquent {
    protected $table = 'groups';
}

用户表的group列上有一个外键,该外键引用了组表上的id.

The user table has a foreign key on the group column which references the id on the group table.

架构可能会帮助

Schema::table('users', function ($table) {

    $table->foreign('group')
        ->references('id')
        ->on('groups')
        ->onDelete('cascade');

});

当我尝试使用以下方法来获得用户组时:

When I try to get the group of a user using:

User::find(1)->group()->name

我得到了错误

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name

根据要求的组架构:

Schema::create('groups', function ($table) {

    $table->increments('id');
    $table->string('name');
    $table->integer('permission');

});

以下是一些屏幕:

用户

表:

用户表FK

编辑

@Cryode在一个聊天会话中为我解决了该问题.问题是我的名字冲突.模型方法使用的名称与列名称(group)相同.只需更改数据库中的列名即可解决此问题.

@Cryode solved it for me in a chat session. The problem was that I had conflicting names. The model method used the same name as the column name (group). Simply changing the column name in the database solved the issue.

推荐答案

在帮助聊天后,问题是存在一个名为group的列,并且关系方法也称为group,因此该表列值优先于关系方法.

After helping through chat, the problem was that there was an existing column called group, and the relationship method was also called group, so the table column value was taking precedence over the relationship method.

重命名关系方法,或将group列重命名为group_id之类都是合适的解决方案(我建议使用group_id路线).

Renaming the relationship method, or the group column to something like group_id are both suitable solutions (I'd suggest the group_id route).

原始答案:

您是通过魔术属性而不是直接从方法中检索组的.

You retrieve the group through a magic property, not directly from the method.

echo User::find(1)->group->name;

如果检索group()方法,它将返回关系对象,不执行任何查询并获取相关对象.

If you retrieve the group() method, it will return the relationship object, not perform any queries and fetch the relating object.

此外,Eloquent将对您的外键列名称进行假设. Group将自动转换为group_id列.如果您有一个名为group的现有列,则应在关系中明确指定该列:

Also, Eloquent will make assumptions as to what your foreign key column names are. Group would automatically translate to a group_id column. If you have an existing column called group, then you should specify that explicitly in your relationship:

public function group () 
{
    return $this->belongsTo('Group', 'group');
}

如果对于属性group收到尝试获取非对象的属性"错误,则您的关系不会返回任何结果($user->group将为NULL).到那时,您应该确保正确设置了您的关系(例如,使用正确的belongsTo,hasOne,hasMany等),并确保您的数据库中确实有一个相关条目.

If you receive a "Trying to get property of non-object" error for the property group, then your relationship is not returning any results ($user->group will be NULL). At that point, you should make sure your relationship is set up properly (e.g. using the correct belongsTo, hasOne, hasMany, etc.), and ensure that you actually have a related entry in your database.

这篇关于Laravel属于抛出未定义的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆