mardi 27 novembre 2018

Laravel 5.6 Route Model Binding with specific columns; How to keep Eloquent relationships

I have set my route-model bindings in boot() function of RouteServiceProvider class as such:

public function boot()
{
    //

    parent::boot();

    Route::bind('itemById', function($id) {
      return \App\Item::where('id', $id)->first(['id', 'name']) ?? abort(404);
    });
}

This returns an eloquent model object with the two columns I want, but none of the relationship methods of the model are returned. If I change the query part of the binding from first(['id', 'name']) to first() it works perfectly.

But I don't want to query all columns on the database for this item and I don't want to filter unwanted columns while returning a response to the webpage on the controller class. How can I make the binding return model relationships alongside desired columns? Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire