vendredi 23 octobre 2015

Laravel 5.1 Eager loading when foreign key is nullable?

I have tables like this:

Account(...)
Country(...)
Region(...)
Station(id, name, lat, lng, ...)
Route(id, name, description, account_id, country_id, region_id, ...)
Trace(id, route_id, length, direction, t1, t2, ...)
TraceNode(id, lat, lng, route_id, trace_id, station_id)

TraceNode.station_id can be nullable.

When I fetch route object, I want to load as much as possible information, all traces, all trace nodes ...

$route = Route::with(array('account', 'account.country','country', 'region', 'traces', 'traces.nodes', 'traces.nodes.stations'))->findOrFail($request->input('id'));

But I get exception telling me that function addEagerConstraint() fails because object is null (station_id is null in this case).

How can I tell laravel to skip those trace nodes where station_id is NULL?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire