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
Related
How can i check to see if the current route is the root page?
i've tried in a blade file typing thie following:
#if(Route::is('/')
//show something
#else
//show something else
#endif
i've also tried it with Route::currentRouteName() such as
#if(Route::currentRouteName() == '/')
//do something
#else
//do something else
#endif
and it doesn't seem to work on the root page.
is there an alternative way? or is it that the root doesn't have a route?
#if(Request::is('/'))
//do something
#else
//do something
#endif
Route::get('/', 'HomeController#index')->name('root');
#if(Route::currentRouteName() == 'root')
//do something
#else
//do something else
#endif
you can use your function to check currentRouteName, you just need to make sure you set the name for that route like I mentioned below:
Route::get('/', 'HomeController#index')->name('home');
#if(Route::currentRouteName() == 'home')
//do something
#else
//do something else
#endif
Alternatively, you can also use routeIs():
#if(request()->routeIs('home'))
// yay
#else
// nay
#endif
The helper works like the normal "is", which means that it supports multiple patterns, but matches against route names instead.
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']))
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
How can i use something like {{#something}} and it will run a controller that checks for "something" so i can return it to translteable text?
My current blade template looks like following:
#layout("layouts.default")
#section("inner")
<h1>Velkommen til pornobiksen</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
I mean, how can i change the "Velkommen til pornobiksen" tekst? I know i can make something like
View::make("template")->with("h1_text","Velkommen til pornobiksen");
But is there not a module/plugin to make it easier? By making like {{#h1_text}} and it will takes from my database or something?
What is the easists way to make this?
You need to use {{ $h1_text }} to put the variable into your blade template.
#layout("layouts.default")
#section("inner")
<h1>{{ $h1_text }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
EDIT
I think I misunderstood you, it seems you are looking for localization
#layout("layouts.default")
#section("inner")
<h1>{{ Lang::get('messages.welcome') }}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection
For localization , you can use helper function : trans
home.php
return [
'welcome' => 'Velkommen til pornobiksen'
];
View
#layout("layouts.default")
#section("inner")
<h1>{{trans('home.welcome')}}</h1>
#foreach($videos as $thumb)
{{$thumb}}
#endforeach
#endsection