这是“评估”表
-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
member表有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');
}
这是我的成员(member)模型
public function evalutions()
{
return $this->belongsToMany('App\Evalution');
}
这是我的代码:
$evalution = Evalution::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');
}
关于php - laravel 数据透视表获取错误记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47288118/