I have this string:
$mystring = "SIZE,DETAIL";
And I´m using:
#if (strpos($mystring, 'SIZE'))
{{ $item->size }}
#endif
#if (strpos($mystring, 'DETAIL'))
{{ $item->detail }}
#endif
But this works fine with SIZE, but not with DETAIL.
What is the problem here?
Since you're using Laravel, you can use str_contains() helper:
#if (str_contains($mystring, 'SIZE'))
The str_contains function determines if the given string contains the given value
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE.
Try this:
#if (strpos($mystring, 'SIZE') !== false)
{{ $item->size }}
#endif
#if (strpos($mystring, 'DETAIL') !== false)
{{ $item->detail }}
#endif
refer: http://php.net/manual/en/function.strpos.php
When using strpos you need to compare to FALSE. An example of your blade code would be:
#if (strpos($mystring, 'SIZE') !== FALSE)
{{ $item->size }}
#endif
#if (strpos($mystring, 'DETAIL') !== FALSE)
{{ $item->detail }}
#endif
However, when using Laravel, you can use str_contains($haystack, $needles) instead of strpos.
Related
Basically I want to #extend a blade template only if it exists, and if not #extend a different template. There are a few stack overflow answers concerning using #if #endif blocks, but that only works if you are #including a file, not #extending. Ideally something like this, but it doesn't work:
#if(some_condition == true)
#extends('one')
#else
#extends('two')
#endif
If the only way is to use Blade directives, could you please provide an example? Thank you!
Try doing it like this:
#extends( $somecondition == true ? 'one' : 'two')
you can use view:exists
#if(View::exists('path.to.view.one'))
#extends('one')
#else
#extends('two')
#endif
You can use View::exists
#if (View::exists('one'))
#extends('one')
#else
#extends('two')
#endif
or
file_exists() and to get the path use resource_path() this,
#if (file_exists(resource_path('views/one.blade.php')))
#extends('one')
#else
#extends('two')
#endif
You can try this, in my case that's working.. See the docs in Laravel - https://laravel.com/docs/5.5/views
#if( file_exists('path to file one'))
#extends('one')
#else
#extends('two')
#endif
you can use the conditional to define the name of the view you want to load and then simply extend it, for example:
#php
$view = '';
if (some_condition == true) {
$view = 'one';
} else {
$view = 'two';
}
#endphp
...
#extends($view)
more info
https://laravel.com/docs/5.5/blade#php
Here is my code, I want to put an array of answers in just one #elseif line
#if( $selectSummaryTable == 'MemType' )
Summary of Members
#elseif( $report_type == ['Category', 'CivilStatus'] )
Baptism Report for {{ $report_date }}
#endif
How do you correctly put multiple values in the #elseif line?
You can use in_array function , change your code from
#elseif( $report_type == ['Category', 'CivilStatus'] )
to
#elseif(in_array($report_type,['Category', 'CivilStatus']))
I have an services in an array, and I have a list of checkbox which have the same values in this array no I need to make each checkbox to be checked if it's in the array
here is my Blade code
{{ Form::checkbox('pro_serves[]', 'pool', null) }} Pool <br/>
{{ Form::checkbox('pro_serves[]', 'gym', null) }} Gym <br/>
{{ Form::checkbox('pro_serves[]', 'maintenance', null) }} Maintenance <br/>
{{ Form::checkbox('pro_serves[]', 'dish', null) }} Dish <br/>
{{ Form::checkbox('pro_serves[]', 'kidsArea', null) }} KidsArea <br/>
{{ Form::checkbox('pro_serves[]', 'parking', null) }} Parking
and here is my Controller
public function editProject($id)
{
$proId = Projects::findOrFail($id);
$proImg = ProjectsImages::where('image_id', $id)->get();
$proPln = ProjectsPlans::where('image_id', $id)->get();
$services = $proId->pro_serves;
$service = explode(',', $services);
return View::make('admin.manageProject.editProject', compact('proId', 'proImg', 'proPln', 'service'));
}
now as you see I am sending the service as an array to the view how can I implement this?
and I welcome any new ideas
Assuming you're either using an old version of laravel, or the laravelcollection/html package, you merely need to pass a third argument.
This is a checked checkbox:
{{ Form::checkbox('pro_servers[]', 'pool', true) }}
So, to have it checked if the value is in the array you can simply do:
{{ Form::checkbox('pro_servers[]', 'pool', in_array('pool', $theArray)) }}
There you go.
This is actually covered in the documentation, which I always recommend taking a look at before posting on SO.
I have role column in users table, and I want to check the value like this in the blade file :
#if ( {{Auth::user()->role }} == '1')
// do something
#endif
Is it possible ?
In blade files, you need to write plain PHP into the #if and others blade statements. So you would need to remove the {{ }}:
#if ( auth()->user()->role == 1)
// do something
#endif
I think you can extend a blade.
https://laravel.com/docs/5.3/blade#extending-blade
It's cool and convenient.
Latest version of Laravel will work with like this. You don't need to use {{}} here.
#if ( Auth::user()->role == 1)
// do something
#endif
#if(\Illuminate\Support\Facades\Auth::user()->hasRole('Admin') == 'Admin')
// do something
#endif
In Laravel blade you can do:
{{ $variable or 'default' }}
This will check if a variable is set or not. I get some data from the database, and those variables are always set, so I can not use this method.
I am searching for a shorthand 'blade' function for doing this:
{{ ($variable != '' ? $variable : '') }}
It is hard to use this piece or code for doing this beacuse of, I do not know how to do it with a link or something like this:
{{ $school->website }}
I tried:
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
But, it does not work. And, I would like to keep my code as short as possible ;)
Can someone explain it to me?
UPDATE
I do not use a foreach because of, I get a single object (one school) from the database. I passed it from my controller to my view with:
$school = School::find($id);
return View::make('school.show')->with('school', $school);
So, I do not want to make an #if($value != ''){} around each $variable (like $school->name).
try this:
#if ($value !== '')
{{ HTML::link($value,'some text') }}
#endif
I prefer the #unless directive for readability in this circumstance.
#unless ( empty($school->website) )
{{ $school->website }}
#endunless
With php 7, you can use null coalescing operator. This is a shorthand for #m0z4rt's answer.
{{ $variable ?? 'default' }}
{{ ($school->website != '' ? '{{ $school->website }}' : '') }}
change to
{{ ($school->website != '') ? '' . $school->website . '' : '' }}
or the same code
{{ ($school->website != '') ? "<a href='$school->website' target='_blank'>$school->website</a>" : '' }}
{{ isset($variable) ? $variable : 'default' }}
I wonder why nobody talked about $variable->isEmpty() it looks more better than other. Can be used like:
#if($var->isEmpty())
Do this
#else
Do that
#endif
From Laravel 5.4, you can also use the #isset directive.
#isset($variable)
{{-- your code --}}
#endisset
https://laravel.com/docs/9.x/blade#if-statements