I check radio button with value from database, it works, but it has rendered "1" just before that checked radio button. If I remove if statement then it does't show.
<?php $formUserType = UserType::all(); ?>
#foreach($formUserType as $valUT)
{{ $varSetRadio = false }}
#if($user->profile->dic_user_type_id == $valUT->id)
{{ $varSetRadio = true }}
#endif
{!! Form::radio('profile[dic_user_type_id]', $valUT->id, $varSetRadio) !!} {{ $valUT->name }}
<br />
#endforeach
{{ expr }} translates to <?php echo e(expr) ?>.
The output of an assignment is the rightmost value in php, so ($var = 1) is equal to 1 (Also true is equal to 1 because PHP is truthy)
Do the following:
#foreach($formUserType as $value)
{!! Form::radio('profile[dic_user_type_id]', $value->id, $user->profile->dic_user_type_id === $value->id) !!}
{{ $value->name }}
<br />
#endforeach
In addition, you shouldn't be comparing anything but simple logic in a view.
Try adding the comparison for dic_user_type to the User model itself, and pass your data in instead of fetching outside of a controller/model.
Related
Consider this block of code from a Laravel blade file:
<div class="result-content">
<h6>{{ $item->name }}</h6>
<div>{!! Str::limit($item->desccription, 120) !!}</div>
#if($item->type == 'lot')
<div>{{ $item->info ? "Current Bid: $".number_format($item->info, 2) : 'No bids yet' }}</div>
<div>{{ \Carbon\Carbon::parse($item->start)->setTimezone($_COOKIE['timezone'])->format('n/j/y g:ia T') }} - {{ \Carbon\Carbon::parse($item->end)->setTimezone($_COOKIE['timezone'])->format('n/j/y g:ia T') }}</div>
#else
<div>{{ $item->info }} Lot{{ $item->info > 1 ? "s" : '' }}</div>
<div>{{ \Carbon\Carbon::parse($item->start)->setTimezone($_COOKIE['timezone'])->format('n/j/y T') }} - {{ \Carbon\Carbon::parse($item->end)->setTimezone($_COOKIE['timezone'])->format('n/j/y T') }}</div>
#endif
<a href="{{ $item->search_url }}" title="">
View: {{ $item->search_title }}
</a>
</div>
Somehow or other, this renders in a browser as a block of text that is all included in a link tag, and "$item->search_url" becomes the link url. But the link is wrong - it just points back to the page this text is appearing on. I need to figure out where "search-url" is assigned its value, so I can fix it. But I am entirely new to Laravel, and I can't figure out where or how "search-url" is getting assigned a value. I've searched the entire system with Visual Code, and that name doesn't appear anywhere else. It's not a variable, it's not a property name, it's not a database field name.
Where is that variable getting assigned?
How would you correctly test an integer value of a view?
Laravel version 5.6.
Consider the code below.
// ViewMatchTest
$response->assertSee($match->goals_away); // checks if "0" is in the view
$reponse->assertSee($match->start_time); // checks if "13:00" is in the view
// match\show.blade.php
<li> {{ $match->goals_away }} </li>
<li> {{ $match->start_time }} </li>
If you would run this test, then it outputs: "OK".
But even if you remove the line:
<li> {{ $match->goals_away }} </li>
The test would still pass because of the "0" in the start_time (13:00) of that match.
I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like:
$cat[0]['id'] = 1;
$cat[0]['name'] = 'First Category';
And so on. In my blade template I have the following code:
{{ $i=0 }}
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
{{ $i++ }}
#endforeach
Which outputs:
0 First Category
1 Second Category
2 Third Category
Notice the numbers preceding the category name. Where are they coming from? Is this some clever Laravel trick? It seems that when you include a counter variable, they are automatically added. I can't find any mention of it anywhere, and I don't want them! How do I get rid of them?
Thanks.
You just need to use the plain php translation:
#foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
#endforeach
EDIT:
Note the $index will start from 0, So it should be {{ $index+1 }}
The {{ }} syntax in blade essentially means echo. You are echoing out $i++ in each iteration of your loop. if you dont want this value to echo you should instead wrap in php tags. e.g.:
<?php $i=0 ?>
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
<?php $i++ ?>
#endforeach
As an additional note, if you choose to work in arrays then thats your call but unless you have a specific reason to do so I would encourage you to work with object syntax, eloquent collection objects in laravel can be iterated over just like arrays but give you a whole lot of extra sugar once you get used to it.
#foreach($cats as $cat)
{{ (isset($i))?$i++:($i = 0) }} - {{$cat['name']}}
#endforeach
<? php $i = 0 ?>
#foreach ( $variable_name as $value )
{{ $ value }}<br />
< ? php $i++ ?>
#endforeach
if your $k is integer you can use {{ $k+1 }} or isn't integer you can use $loop->iteration
// for laravel version 4 and after
#foreach ($posts as $k => $post)
{{ $loop->iteration }}. {{ $post->name }}
#endforeach
You can actually use a built in helper for this: {{ $cat->incrementing }}.
I would like to test the existence of value in blade on Laravel 4.
Like:
{{ Form::text('name', #if(isset($value)) {{$value}} #endif; }}
I tried this:
{{ Form::text('name', #if(isset($value)) $value #endif; }}
You can do it in one line.
I'd prefer a better approach, using condition?if:else , which also I use in my daily projects:
{{ Form::text('name',isset($value)?$value:'') }}
or even:
{{ Form::text('name',isset($value)?$value:null) }}
Anywhere before that, you can add the line <?php if(!isset($value)) { $value= ''; } ?>. Then you can proceed as usual {{ Form::text('name', $value) }}
1st question:
I've inserted the localization in many types of texts and things, but I don't know how to import it into in the following forms:
{{ Form::label('name', 'here') }}
{{ Form::text('name', null, array('placeholder' => 'here')) }}
{{ Form::submit('here', array('class' => 'btn btn-success')) }}
{{ Form::button('here', array('class' => 'btn btn-default')) }}
I want it to be in the form label 'here' and in the placeholder of the text 'here'.
2nd question:
I am not allowed to insert it with links in my language file: text here blah blah BLAH?
Is there anyway to insert it with links?
Thanks in advance.
Supposing that your messages are stored in app/lang/en/message.php you can use the same way for all your cases:
In Blade template:
{{ Form::label('name', Lang::get('message.key')) }}
{{ Form::text('name', null, array('placeholder' => Lang::get('message.key'))) }}
{{ Form::submit(Lang::get('message.key'), array('class' => 'btn btn-success')) }}
In HTML tag mixed with some Blade expression:
BLAH
You can also use localization in Blade templates using strictly Blade syntax.
Given that you have a message in your /app/lang/en/messages.php that corresponds to the key "message_key", you can do :
#lang('messages.message_key')
to render the message in the locale that your application is configured to use.
So, The answers for both of your questions are:-
1) {{ Form::label('name', 'here') }}
Here, you need to change the "here" text hence laravel localization method can be used.For eg:-
{{ Form::label('name', '__("Here")' }} or
{{ Form::label('name', '__('message.here') }}.
2)< a href="{{ URL::to('text') }}">BLAH< /a>
Here, you need to change the label instead of link.
< a href="URL::to('text') ">{{__('message.BLAH')}}< /a>.
This is the simplest which works for me !
{!! Form::label('title', trans('users.addNewRecordsNameFieldLabel'),['class' => 'control-label']) !!}
I feel, already blade parsing is started the moment {!! or {{ is started
A possible answer to your second question:
You could set up a language file resources/lang/en/page.php like this:
return [
'sentence' => 'A sentence with a :link in the middle.',
'link_text' => 'link to a another page'
];
And use it in a Blade template like this:
{!! trans('page.sentence', [
'link' => '' . trans('page.link_text') . ''
]) !!}}
The result would be:
A sentence with a link to a another page in the middle.
You alse can use __() method
{{ __('app.name') }}