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 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单