I'm using Blade templating in a Laravel 4 view to display a form for both create and edit, it all works fine until I have to render a select
input type - it's ok when editing, but when creating a new record I get an error message Undefined variable: data
, which is triggered by the 'selected' value in the second last line (last parameter passed to Form::select()
)
@if( isset( $data) )
{{ Form::model( $data ) }}
@else
{{ Form::open() }}
@endif
{{ Form::label( 'foo', 'label text' ) }}
{{ Form::select( 'foo' , array('0' => 'No', '1' => 'Yes'), $data->foo ); }}
{{ Form::close() }}
is there a standard way in Laravel to avoid this when $data
is not defined, using model binding as I have so far? Or do I need to simply use a variable to check in advance if $data
is not undefined?
The whole purpose of Form Model Binding is that you don't have to set the values on your input fields yourself. You can just leave the value argument blank and Laravel will fill the value(s) in based on the name
of the select, input, etc.
{{ Form::select( 'foo' , array('0' => 'No', '1' => 'Yes')); }}
Also you can even remove your isset($data)
check because Laravel will do that as well:
{{ Form::model( $data ) }}
{{ Form::label( 'foo', 'label text' ) }}
{{ Form::select( 'foo' , array('0' => 'No', '1' => 'Yes')); }}
{{ Form::close() }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.