php - Cannot select columns in advanced ::with() statement -
i have problem selecting columns (mysql select statement) advanced query::with
statement.
property::where('public_id', $publicid)->with(['owner' => function ($query) { $query->select('email')); }])->first();
the returned owner object null if add select
statement, , full user
model if don't add select('...')
statement.
the queries executed correct , return desired data if run them phpmyadmin:
select * `properties` `public_id` = '53f1c59cefe65' limit 1; select `email` `users` `users`.`id` in ('54');
data without select:
{"id":4,"owner_id":54,"owner":{"id":54,"username":"ckertzmann","email":"ckertzmann@gmail.com","permissions":[],"activated":true,"activated_at":null,"last_login":null,"first_name":"daisy","last_name":"haag","created_at":"2014-08-18 09:21:26","updated_at":"2014-08-18 09:21:26"}}
data select:
{"id":4,"owner_id":54,"owner":null}
you need select keys eloquent knows how match related models respective relation parents.
property::where('public_id', $publicid)->with(['owner' => function ($query) { $query->select('email', 'owner_id'); }])->first();
Comments
Post a Comment