php - Laravel: Is it bad practice to pass models to views? -


in examples have seen far, concrete values passed views - separate variables, or arrays.

example laravel documentation:

$view = view::make('greeting')->with('name', 'steve'); 

is bad idea pass model view?

in controller use:

return response->view('quote.render', quote::find($id)) 

instead of like:

return response->view('quote.render',    ['date' => $quote->date,'clientname' => $quote->client->name, 'items'=> $quote->items]) 

and in view (blade template) can use model this:

to: {{$quote->client->name} date: {{$quote->date}} 

the advantage me instantly have model's data on hand - , if model changes (get's more attributes) not have change controller pass new data... feels cleaner.

are there pitfalls approach? or reasons bad practice? feels right - don't see in examples.

it fine pass model view, way this:

public function show($id) {     $thing = thing::findorfail($id);      return view('showathing')->with('thing', $thing); } 

using findorfail() in context throw 404 error if item doesn't exist in database. useful because if tried render view $thing = null you'd run un-handled exceptions.


Comments