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.
Related
I simply want to give the text element of my edit-form a class.
{{ Form::text('first_name') }}
I know that i have to use an array with the class of the field as third argument. But i do NOT want to give a second argument. This one
{{ Form::text('first_name' , ' ' , array('class' => 'form-control')) }}
applys the class correctly, but sets the dafault value to an empty string. This is a problem, because now laravels auto-complete function doesn't fill the form with the correct data from the database. Is there a way to give no second argument or setting it to default like this?
{{ Form::text('first_name' , default , array('class' => 'form-control')) }}
Thanks!
Okay, i solved it myself. It is as easy as it always is. Simply use
{{ Form::text('first_name' , null , array('class' => 'form-control')) }}
I just started using the Laravel Collective Form Service Provider/FormBuilder.
I use this custom component for rendering a text field with label. The problem is that I am trying to translate it's label with the __() function but the $name variable gets tansformed from first_name to First Name in a really late stage.
<div class="control-group">
{{ Form::label($name, null, ['class' => 'group__label']) }}
{{ Form::text($name, $value, array_merge(['class' => 'control-
group__control'], $attributes)) }}
</div>
I can't simply do this:
{{ Form::label(__($name), null, ['class' => 'group__label']) }}
Again, because it gets first_name and later transforms it into First Name. My nl.json file contains a translation for First Name, not first_name.
If I add the _() translate function to the last rule of the label method in FormBuilder is, then it solves my problem. But ofcourse, I don't want to modify vendor code!
return $this->toHtmlString('<label for="' . $name . '"' . $options . '>' . __($value) . '</label>');
So. How to solve this issue? Do I, somehow, need to create a custom FormBuilder->label() method?
Use trans() helper:
{{ Form::label(trans($name), null, ['class' => 'group__label']) }}
Using name of field with trans() can create errors when the language changed.
you should use somthing like this
{{ Form::label('name', trans(''), ['class' => '']) }}
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']) !!}
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')) !!}
I have this:
<title>{!!Config::get('lang_en.title')!!}</title>
I want to do somethnig like this:
<title>{!!Config::get('lang_{{$language}}.title')!!}</title>
Is it possible ?
try this
{!!Config::get('lang_' . $language . '.title')!!}
by removing curly brackets and append the value as a string
I think the general thing you need to understand is that anything inside {!! !!} is normal PHP, not Blade templating.
So you would concatenate variables the same way you would in PHP, as your accepted answer shows:
'lang_' . $language . '.title'
Basically {!! X !!} gets converted to <?php echo X ; ?>
And {{ X }} gets converted to <?php echo htmlentities( X ); ?>
{{ }} is safer and should always be used if the string contains user input. {!! !!} should be used if the string contains HTML. What if it's a mix of both?
Then you should use {!! !!} so that the HTML works, but wrap the user input with the e() function, e.g.
// In the controller
$string = '<span>' . e($username) . '</span>';
// In the view
{!! $string !!}
e() is just Laravel shorthand for htmlentities().