I have an issue with Eager Loading using the below code.
Issue is with the Status model
$ticket = Ticket::where('id', $id)->with(['customer', 'status', 'notes' => function($query) {
$query->orderBy('updated_at', 'desc');
}])->first();
If I do,
return response()->json($ticket);
I get the expected response, all OK
{"id":1,"customer_id":10001,"ztk_ticket_no":"ZTK0000001","status":{"id":1,"value":"Open","deleted_at":null,"created_at":"2016-02-13 01:36:20","updated_at":"2016-02-13 01:36:20"},"deleted_at":null,"created_at":"2016-02-13 01:36:20","updated_at":"2016-02-13 01:36:20","customer":{"id":1,"customer_id":10001,"title":"Test Company","deleted_at":null,"created_at":"2016-02-13 01:36:20","updated_at":"2016-02-13 01:36:20"},"notes":[{"id":1,"ticket_id":1,"note":"Lorem ipsum dolor sit amet, ","status":1,"deleted_at":null,"created_at":"2016-02-13 01:36:20","updated_at":"2016-02-13 01:36:20"}]}
But if I do
return response()->json($ticket->status);
I get the id of the status, not the Model
1
Status Model:
class Status extends Model
{
protected $table = 'statuses';
}
Ticket Model:
class Ticket extends Model
{
public function status() {
return $this->hasOne('App\Status', 'id', 'status');
}
}