繁体   English   中英

laravel数据透视表获得错误的记录

[英]laravel pivot table get wrong records

这是“评估”表

-id
-title
-slug

这是“成员”表

-id
-name
-surname

这是“ evalution_member”数据透视表

-evalution_id
-member_id

评估表有以下3条记录

id | title | slug
1 | a | a
2 | b | b
3 | c | c

成员表有10条如下记录

id | name | surname
1 | xx1 | xx1
2 | xx2 | xx2
3 | xx3 | xx3
4 | xx4 | xx4
5 | xx5 | xx5
6 | xx6 | xx6
7 | xx7 | xx7
8 | xx8 | xx8
9 | xx9 | xx9
10 | xx10 | xx10

evalution_member表具有10条记录,如下所示

evalution_id |member_id
1 | 1
1 | 2
1 | 3
3 | 4
3 | 5
3 | 6
3 | 7
3 | 8
3 | 9
3 | 10

这是我的评估模型

public function members()
{
    return $this->belongsToMany('App\Member');
}

这是我的会员模特

public function evalutions()
{
    return $this->belongsToMany('App\Evalution');
}

这是我的代码:

$ evalution = Evaluation :: find($ id)-> with('members')-> first();

从此代码返回成员列表,但记录错误。 正确的记录必须从第4条开始,并在最后一项结束,但返回列表包含前3条记录。

如果我按以下方式更改评估模型:

public function members()
{
    return $this->belongsToMany('App\Http\Models\Member', 'evalution_member', 'evalution_id', 'member_id');
}

返回列表仅包含1条记录,这些记录是evalution_member表上的最后一条记录。

为什么呢?

那里有什么问题?

将数据透视表名称添加到这样的方法中。

public function members()
{
    return $this->belongsToMany('App\Member', 'evalution_member');
}

暂无
暂无

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

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