从一个表中访问不属于另外两个表的数据

时间:2016-12-05 12:48:17

标签: php mysql laravel-5

//示例数据库

起初学者有1,2,3,4,5,6 ids

Scholar      
Id      
1   
2
3
4
5
6

现在1和2是在表会员

下进行的
Member 
Id
1 - taken
2 - taken

接下来的3和4是在表AddRequest

下进行的
AddRequest
Id
3 - taken
4 - taken

现在学者表应该只有我要显示的ID 5和6。

如何使用下面的代码解决此问题

public function ListOrgaScholar($ship_id)
{
    $members = Member::where('ship_id','=',$ship_id)->get();
    $members = $members->toArray();
    $scholar_ids = array_pluck($members, 'scholar_id');
    $scholar_exits = Scholar::whereNotIn('scholar_id', $scholar_ids)->get();

    $requests = AddRequest::where('ship_id','=',$ship_id)->get();
    $requests = $requests->toArray();
    $scholar_ids = array_pluck($requests, 'scholar_id');
    $add_exits = Scholar::whereNotIn('scholar_id', $scholar_ids)->get();   

    if ($scholar_exits == true && $add_exits == true) {

    $scholars = (new Scholar)->newQuery()->select('*');

    $scholars = $scholars->get();
    dd($scholars);
}else{
}
}

dd的结果

Collection {#302 ▼
  #items: array:7 [▼
    0 => Scholar {#305 ▶}
    1 => Scholar {#306 ▶}
    2 => Scholar {#307 ▶}
    3 => Scholar {#308 ▶}
    4 => Scholar {#309 ▶}
    5 => Scholar {#310 ▶}
    6 => Scholar {#311 ▶}
  ]
}

This for Member and AddRequest Table

This for Scholar

0 个答案:

没有答案