我遇到的问题分为两部分:
我被困在#1上。我跑的时候
$favorites = Event::with('rsvps')->get();
我收到包含rsvps的所有事件,但是我无法根据rsvps信息对其进行过滤。
[
{"id":1,"user_id":"1","created_at":"2016-03-01 02:26:48","updated_at":"2016-03-01 02:26:48","event_name":"Awesome Event","event_venue":"The Rainbow Room","address":"612 Rachael Ln.","event_details":"Epic","event_date":"03\/12","image":"2016-03-01-02-26-48-heartHands.jpg","time":"8:00",
"rsvps":[
{"id":1,"created_at":"2016-03-01 02:28:39","updated_at":"2016-03-01 02:28:39","event_id":"1","user_id":"1"}]
},
{"id":2,"user_id":"1","created_at":"2016-03-01 02:40:05","updated_at":"2016-03-01 02:40:05","event_name":"New Event","event_venue":"My House","address":"612 Rachael Ln.","event_details":"Epic","event_date":"03\/01","image":"2016-03-01-02-40-05-img3.jpg","time":"8:00",
"rsvps":[
{"id":2,"created_at":"2016-03-01 02:40:12","updated_at":"2016-03-01 02:40:12","event_id":"2","user_id":"1"},
{"id":3,"created_at":"2016-03-01 22:21:33","updated_at":"2016-03-01 22:21:33","event_id":"2","user_id":"2"}
]}
]
当我运行测试以获取带有user_id = 1的rsvps:
时$favorites = Event::with('rsvps')->find('id')->where('rsvps->user_id', '1')->get();
它给了我错误:调用成员函数where()on null
有人可以告诉我一些我做错的线索吗?
答案 0 :(得分:0)
您需要查询关系rsvps
。您正在尝试查询events
。
$favorites = Event::whereHas('rsvps', function($q) {
$q->where('user_id', 1)
})->find('id'); // Passing in 'id' won't work here, it should be the actual id of an event.