dongshao2967 2014-07-29 22:37
浏览 37
已采纳

Laravel属于抛出未定义的错误

I have 2 models.

User:

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'users';

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

And Group:

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

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

Schema may help

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

I get the error

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

Group Schema as requested:

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

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

});

Here are some screens:

users table

enter image description here

groups table:

enter image description here

And the users table FK

enter image description here

EDIT

@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.

  • 写回答

1条回答 默认 最新

  • douweinu8562 2014-07-29 22:40
    关注

    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.

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


    Original answer:

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

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

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

    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');
    }
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄