The best way to do this is to use model form binding ( http://four.laravel.com/docs/html#form-model-binding ):
Use an existing model or create an "empty" model class:
class NoTable extends Eloquent { protected $guarded = array(); }
Find your model or create an instance of your empty class and fill it with data:
public function getSampleform() { // Load database data here $model = new NoTable; $model->fill(['name' => 'antonio', 'amount' => 10]); return View::make('sampleform')->with(compact('model')); }
If you will use your form with a table on which you already have data, here is how you use it:
public function getSampleform() {
To fill out your form, use model form binding, this is an example in Blade:
{{ Form::model($model, array('route' => array('sample.form')) ) }} {{ Form::text('name') }} {{ Form::text('amount') }} {{ Form::close() }}
You donβt even have to pass your input, because Laravel will populate your inputs using the first:
1 - Session Flash Data (Old Input) 2 - Explicitly Passed Value (wich may be null or not) 3 - Model Attribute Data
And Laravel will also take care of the csrf token for you using Form :: open () or Form :: model ().
Antonio Carlos Ribeiro
source share