vimuth
vimuth

Reputation: 5612

laravel prefil form inputs from controller

Hi I'm using laravel forms.

{!! Form::text('product_name', null, array('class' => 'form-control date_pick')) !!}

Is there a way to set this default input from controller?. Think we can do it with Flash but I couldn't find an example. I want to take vlues from a model and prepopulate.

$products = Products::all();

It would be great if someone know how to do this. What is the easiest way to do it?

Upvotes: 1

Views: 57

Answers (1)

lewis4u
lewis4u

Reputation: 15027

This is done with a basic edit route where you simply use:

In ModelController you would call the edit page:

public function edit(ModelName $model) {

    return view('name.of.the.blade.view', compact('model')); // if using compact then without dollar symbol
}

In blade view simply make the form with all the input fields you have for that model:

{!! Form::model($model, ['method' => 'PATCH', 'route' => ['model.update', $model->id],]) !!}

    Now all the form fields will have the values from that model.


{!! Form::close() !!}

And the edit and update route (inside routes/web.php) would be like this:

Route::get('/model/{model}/edit', 'ModelController@edit')->name('model.edit');
Route::patch('/model/{model}', 'ModelController@update')->name('model.update');

Upvotes: 1

Related Questions