Search code examples
laravel-5laravel-form

laravel 5 assign variables to forms fields


I want to set the placeholder value for my form element. Here is my controller:

class MyController extends Controller {
 public function index(Request $request){
   $start_time = $request->get('start_date');
   return view('showChart',compact('start_time'));
 }
}

The controller will send response to showChart.blade.php. Part of the showChart.blade.php is:

{!! Form::open(array('url' => '/', 'class' => 'form')) !!}
{!! Form::label('START DATE') !!}
            {!! Form::text('start_date', null, 
                      array('required',  
                        'placeholder'=>{{start_time or 'default value'}}
                      ))
            !!}
{!! Form::close() !!}

It does not work as expected, it will output: , it seems that the code does not execute.


Solution

  • You can’t use template delimiters once inside an already-open set. You’ll have to do something like this instead:

    {!! Form::text('start_date', null, ['placeholder' => empty($start_date) ? 'default value' : $start_date, 'required' ]) !!}