mercredi 2 mars 2016

Drill down into laravel eloquent relationships

I have a problem that’s broken into 2 parts:

1) I’m trying to get the specific events where rsvps = the logged in user_id.

2) And then filter that where event_date <= today to get the past events.

I’m getting stuck on #1. When I run

$favorites = Event::with('rsvps')->get(); 

I get all the events that include the rsvps which is good but I can’t filter it based on the rsvps info.

[
{"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"}
]}
]

When I run a test to just get the rsvps with the user_id = 1:

$favorites = Event::with('rsvps')->find('id')->where('rsvps->user_id', '1')->get();

it gives me the error: Call to a member function where() on null

Can someone please give me a clue as to what I'm doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire