How can I echo the value of the database value into input type?
Here's the database variable
$suppliers->supplier_name
And here my form text code. this code is not working...
{!! Form::text('supplier_name', '', array('class' => 'form-control','value'=>'$suppliers->supplier_name')) !!}
I'm using laravel 5.1
Second parameter is the value for the input, so:
{!! Form::text('supplier_name', $suppliers->supplier_name, array('class' => 'form-control')) !!}
Related
I'm building a simple form with a username and password.
When I construct a username field like this:
{{ Form::text('username') }}
My page loads without issue. As soon as I want to define a class like this:
{{ Form::text('username', ['class' => 'awesome']) }}
I get an error, like so:
ErrorException in HtmlBuilder.php line 65:
htmlentities() expects parameter 1 to be string, array given
Haven't been able to find any info regarding this error online. These examples are taken STRAIGHT from the LaravelCollective documentation.
Any ideas? Thanks!
You should pass the class in the third parameter like so:
{{ Form::text('username', null, ['class' => 'awesome']) }}
or:
{{ Form::text('username', '', ['class' => 'awesome']) }}
The second parameter is the value field
If you are using Laravel 5 You should write like this:
{!! Form::text() !!}
Not like this:
{{ Form::text() }}
Also you have attributes array given as second parameter which is default value of this input. Please change it to:
{!! Form::text('your_name', null, ['class' => 'someclass']) !!}
Looking at the source code, the expected parameters are:
Form::text($name, $value = null, $options = [])
The second parameter is for specifying a default value for the form element. So to define a class, you should pass the array to the third parameter:
Form::text('username', null, ['class' => 'awesome'])
If you are using laravel 5.4
you shouldn't use double curly for laravel collective html form like this
{{ Form::() }}
try this instead
{!! Form::text('username', $value = null, ['class' => 'awesome']) !!}
i am still learning how to work with laravel.
Now i have learned how to make a form in laravel. But i have some trouble to echo a variable.
What i want to do is: i want to echo a variable as the value of an input-field if this variable exists, otherwise it should echo nothing.
so my form line looks like this
{{Form::text(
'league_name',
'#if(isset($varialble) {{$variable}} #else {{$nothing}} #endif)'
)}}
How can i echo a variable in a form?
i am using blade btw.
This is the right way to do it:
{!! Form::text('league_name', isset($varialble) ? $variable : '') !!}
If you're using PHP7, you can do this:
{!! Form::text('league_name', $varialble) ?? '') !!}
Update
To add placeholder, pass it in an array as third parameter. Also, you usually want to pass a class:
{!! Form::text('league_name', isset($varialble) ? $variable : '', ['placeholder' => 'My placeholder', 'class' => 'form-control']) !!}
Try This. Here You can use null instead of $nothing and $variable is your variable
{{Form::text('league_name',$varialble ? $variable : $nothing)')}}
In Laravel use can use normal PHP code also
{!! Form::text('league_name', !empty($varialble) ? $variable : '') !!}
here you can use {!! data/variables !!} or {{ data/variables }}
above will resolve your problem.
what I am trying to do is to convert standard Blade select form with multiple options where you must select options with ctrl pressed to one where you use checkboxes to select multiple choices.
My form look like this:
{!! Form::open(array('action' => 'UserController#visit_step_two', 'method' => 'post')); !!}
{!! Form::select('services', $servicesList, null, array('multiple'=>'multiple','name'=>'services[]')); !!}
{!! Form::submit('Next'); !!}
{!! Form::close(); !!}
What should I do to change that?
The Form::select() receives an array or collection which it then iterates over to generate all the <select> element's options. If you want to use checkboxes instead, you need to iterate manually to create each checkbox:
{!! Form::open(array('action' => 'UserController#visit_step_two', 'method' => 'post')); !!}
#foreach ($servicesList as $value => $name)
{!! Form::checkbox('services[]', $value, ['id' => 'service' . $value]); !!}
{!! Form::label('service' . $value, $name) !!}
#endforeach
{!! Form::submit('Next'); !!}
{!! Form::close(); !!}
The above will create a form with a list of checkboxes that have labels attached. The id attribute is added so that you can also click on the label to select the associated checkbox, since the first parameter of Form::label() will be used to generate the for attribute of the <label> which is associated with a checkbox <input> field that has the same value for the id, and since an id has to be unique it is generated using the value like so 'service' . $value because all values should also be unique.
You need jquery plugin to do that, or you can write your own.
I know a plugin is Multiple Select http://wenzhixin.net.cn/p/multiple-select/docs/#checkall-uncheckall
I am using the HtmlServiceProvider in laravel 5.1 to build my form , I want to set the selected option in select dropdown list tag when I get the data from my model class
My code so far:
{!! Form::model($advertisment,['url'=>'dashboard/edit/advertisment']) !!}
{!! Form::select('Gender', [""=>'Select Gender',"Male"=>'Male', "Female"=>'Female'],0, ['class' => 'form-control', 'id' => 'form_control_6' ,'required']) !!}
{!! Form::close() !!}
Set the 0 parameter to "Male" for example and you will figure it out
If you don't want to override the value from the model, pass in null
I have seen similar questions asked before, but was unable to find a solution to my problem. So I have a ClientController and within it this function
public function edit(Client $client)
{
return view('clients.edit', compact('client'));
}
So that passes the client object to my edit view. This view is pretty much the following
{!! Form::model($client, ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
#include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}
So it is using a partial for the form. At the moment, the partial looks like so
<div class="form-group">
{!! Form::label('clientName', 'Client Name:') !!}
{!! Form::text('clientName') !!}
</div>
<div class="form-group">
{!! Form::label('clientStatus', 'Client Status:') !!}
{!! Form::select('clientStatus') !!}
</div>
When I visit the edit page for a client, I can see the form. The clientName is populated with the clientName value. The clientStatus is populated if I put it as a text input, but I cant get it to populate within a select as shown above. Furthermore, clientStatus can either be New or Existing. I need the select box to be pre-populated with the status of the client that is being edited, but I need the other option available within the select as well. So if the clientStatus is New, New should be pre-selected within the select box and if I open the select, Existing should be the other option.
What would be the best way to achieve this?
Thanks
Modify your select to include an array of the possible values.
Basic Select - Label is value
{!! Form::select('clientStatus',['New','Existing']) !!}
Key Value Select - Key is value
{!! Form::select('clientStatus',[ 1 => 'New', 2 => 'Existing']) !!}
Form model will then set the value in the select to the one in the model.
More information see the docs.