【发布时间】:2017-01-18 19:06:39
【问题描述】:
我在我的 Laravel 项目中使用 https://github.com/nicolaslopezj/searchable。
我有一个与用户有多对多关系的 Hotel 对象。
我可以使用这个查询关系:
protected $searchable = [
'columns' => [
'hotels.nombre' => 10
],
'joins' => [
'hotel_user' => ['hotels.id' , 'hotel_id'],
'users' => ['hotel_user.user_id', 'users.id']
]
];
搜索返回类似这样的内容(json 格式)
[
{
"id": 3,
"nombre": "Paradisus Playa del Carmen La Esmeralda",
"url_foto": "uploads/hotel/3/1484747239.jpg",
"website_url": "https://www.melia.com/es/hoteles/mexico/playa-del-carmen/home.htm",
"updated_at": "2017-01-18 13:47:44",
"relevance": 60,
"users": [
{
"id": 1,
"first_name": "Alex",
"last_name": "Angelico",
"formatted_name": "Alex",
"company": "ConexionBIZ",
"title": "Director",
"picture_url": "https://media.licdn.com/mpr/mprx/0_FIUn3Wf5E4OMEwzR5feW3o7IoRSveSkR5W7d3oxczOM5BdPUwDws7EIJXvDEIE5c6HMeaSSFgb19",
"created_at": "2017-01-17 12:00:00",
"updated_at": "2017-01-18 13:50:19",
"pivot": {
"hotel_id": 3,
"user_id": 1,
"created_at": null,
"updated_at": null
}
}
]
},
我想过滤属于某个特定用户的所有酒店。 我试过了:
$result = \App\Hotel::search($request->get('q'))->with([
'users' => function($q) use ($user_id) {
$q->where('users.id', '<>', $user_id);
},
])->get();
但这会返回没有相关用户的酒店。我需要从结果中删除酒店(不仅仅是用户)。
我该怎么做?
非常感谢!
【问题讨论】: