简体   繁体   English

Laravel 所属关系返回一个 id 而不是 model

[英]Laravel belongstomany relationship returns an id instead of a model

I'm learning laravel, but i've ran into a problem and i can't seem to get it right.我正在学习 laravel,但我遇到了一个问题,我似乎无法正确解决。 I have two models:我有两个模型:

Model 'Group': Model“组”:

 Schema::create('groups', function (Blueprint $table) {
        $table->increments('id')->unique();
        $table->string('name')->unique();
        $table->text('description')->nullable();
        $table->string('picture')->nullable();
        $table->timestamps();
    });

and model 'User':和 model '用户':

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->string('password');
        $table->timestamp('email_verified_at')->nullable();
        $table->timestamps();
        $table->rememberToken();
    });

These models should have a many-to-many relationship, so i've created a pivot table named 'group_user':这些模型应该有一个多对多的关系,所以我创建了一个名为“group_user”的 pivot 表:

Schema::create('group_user', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('group_id')->unsigned();
        $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');

        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        $table->timestamps();
    });

Next i defined the many-to-many relationship:接下来我定义了多对多关系:

In the 'Group' model:在“组”model 中:

 public function members()
{
    return $this->belongsToMany('App\User', 'group_user', 'group_id', 'user_id')->withTimestamps();
}

In the 'User' model:在“用户”model 中:

public function groups() {
    return $this->belongsToMany('App\Group', 'group_user', 'user_id', 'group_id')->withTimestamps();
}

In my database i have a pivot record, that links group 1 to user 1:在我的数据库中,我有一个 pivot 记录,它将组 1 链接到用户 1:

+----+----------+---------+------------+------------+
| id | group_id | user_id | created_at | updated_at |
+----+----------+---------+------------+------------+
|  1 |        1 |       1 | NULL       | NULL       |
+----+----------+---------+------------+------------+

So far so good, but when i try to print the members of group 1:到目前为止一切顺利,但是当我尝试打印第 1 组的成员时:

@foreach($group->members() as $user)
   <p>{{ $user->first_name }}</p>
@endforeach

i get this error:我收到此错误:

Trying to get property 'first_name' of non-object

When i print the user object like this当我像这样打印用户 object

 @foreach($group->members() as $user)
   <p>{{ $user }}</p>
@endforeach

I get the result 1 , which is the user_id of the member.我得到结果1 ,这是成员的 user_id。 Can anyone point out where i messed up?谁能指出我在哪里搞砸了? Been looking at it for hours, but i just can't see it.看了好几个小时,还是看不出来。 Thanks in advance!提前致谢!

try $group->members instead of $group->members() .尝试$group->members而不是$group->members() Because $group->members() return the relationship builder.因为$group->members()返回关系构建器。

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

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