I have a route that just shows some form:
Route::get('/form/{form}', 'FormController@show');
In the FormController, thanks to type injection I automagically convert form id (from {form}) to App\Form object and display it in the view:
public function show(Form $form){
return view('form', compact('form'));
}
When I get a Form object through Eloquent query builder, I can eager load related elements/models like that:
$form = App\Form::where('id', '1497')->with('subform')->get()
Is there a way to automatically eager-autoload $form object with subform related object, or do I need to do it manually like that:
public function show($id){
$form = App\Form::where('id', $id)->with('subform')->get();
return view('form', compact('form'));
}
You can add relation by using
load()method: