简体   繁体   English

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

[英]laravel pivot table get wrong records

this is "evalution" table 这是“评估”表

-id
-title
-slug

this is "member" table 这是“成员”表

-id
-name
-surname

this is "evalution_member" pivot table 这是“ evalution_member”数据透视表

-evalution_id
-member_id

evalution table has 3 records as below 评估表有以下3条记录

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

member table has 10 records as below 成员表有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 table has 10 record as below 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

this is my Evalution model 这是我的评估模型

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

this is my Member model 这是我的会员模特

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

And this is my code : 这是我的代码:

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

Return member list from this code but coming wrong records . 从此代码返回成员列表,但记录错误。 Right records must start on 4th item and end on last item but return list contains first 3 records. 正确的记录必须从第4条开始,并在最后一项结束,但返回列表包含前3条记录。

if i change Evalution model as below : 如果我按以下方式更改评估模型:

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

return list contain only 1 record that is last record on the evalution_member table. 返回列表仅包含1条记录,这些记录是evalution_member表上的最后一条记录。

why ? 为什么呢?

what is the problem in there ? 那里有什么问题?

Add pivot table name to methods like this. 将数据透视表名称添加到这样的方法中。

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

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

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