How do I include a file based on which route "category" is used with blade with Laravel? I'm basically trying to do this in the sub nav part of my view template:
app/views/layouts/default.blade.php
#if (Route::resource() == 'tasks')
#include('navs.task')
#elseif (Route::resource() == 'projs')
#include('navs.proj')
#elseif (Route::resource() == 'miscs')
#include('navs.misc')
#else
#include('navs.info')
#endif
but that throws the error "Undefined class constant 'resource' "
Very late, but i think using Request::is is an easier way.
#if (Route::is('tasks'))
#include('navs.task')
#elseif (Route::is('projs'))
#include('nav.proj')
#elseif (Route::is('miscs'))
#include('nav.misc')
#else
#include('nav.info')
#endif
Also this is alot of if else's use Switch case instead.
You're after Route::currentRouteName().
#if (Route::currentRouteName() == 'tasks')
#include('navs.task')
#elseif (Route::currentRouteName() == 'proj')
#include('navs.proj')
#elseif (Route::currentRouteName() == 'misc')
#include('navs.misc')
#else
#include('navs.info')
#endif
Michael P: your answer led me to a correct answer. Route::currentRouteName() returns 'task.index' or 'proj.create' which is too granular. Route::current()->getUri() works.
#if (Route::current()->getUri() == 'tasks')
#include('nav.task')
#elseif (Route::current()->getUri() == 'projs')
#include('nav.proj')
#elseif (Route::current()->getUri() == 'miscs')
#include('nav.misc')
#else
#include('nav.info')
#endif
Related
Route is not working. How can I write an anchor tag based on some condition? If the condition is true then the anchor tag will be printed with the route. I wrote the code below, but I get an error.
In my blade template:
#php
if (SOMECONDATION) {
echo 'Approve';
}
#endphp
{{ }} is echo-ing. You can use this :
#if($p->name == 0)
Approve
#endif
No need to use echo or anything. Directly use #if #endif and for route use {{ }}
#if($condition)
Approve
#endif
Theres any Solution to hide an Code in Laravel for one Page?
With this i can display only one requested page ,Example:
#if (Request::path() == 'message/send')
#endif
Theres an opposite to hide an code for an path?
I have an URL also with after send an Username:
message/send/username
I have try: #if (Request::path() == 'message/send/') and #if (Request::path() == 'message/send/{{$username}}') but dont work.
Thanks
Use request()->is() for this:
#if (request()->is('message/send*'))
you can use segment method in laravel
#php $member = Request::segment(1);#endphp
#if($member =='member')
<style type="text/css"> .test{color: #1a393a !important;</style>
#endif
#endif
Here my first segment is member
The controller is the proper place to handle request specifics. However, if you still wish to do this in your blade view:
#if (Request::path() == 'message/send/' . $username)
You do not use template syntax {{ }} inside expressions. When the blade template is compiled, #if (expr) is converted to <?php if (expr): ?>, so standard PHP applies within the expression.
Route path comparison doesn't work that way. For example:
message/send/jwz104 will never be equal to message/send/{{$username}}
You could compare the route name:
#if(\Request::route()->getName() == 'message.send')
#endif
This also isn't a nice solution. You should handle this in your controller or by view composers.
with core php-
#if (strpos($_SERVER['REQUEST_URI'], "message/send") !== false)
#endif
I have a custom view and in some functions, I used paginate and other functions I don't use paginate. now how can I check if I used paginate or not ?
#if($products->links())
{{$products->links()}}
#endif // not work
of course that I know I can use a variable as true false to will check it, But is there any native function to check it ?
This works perfectly. Check if $products is an instance of Illuminate\Pagination\LengthAwarePaginator then display the pagination links.
#if($products instanceof \Illuminate\Pagination\LengthAwarePaginator )
{{$products->links()}}
#endif
#if($threads->hasPages())
{{ $threads->links() }}
#endif
Simple one!
Try like this
#if($products instanceof \Illuminate\Pagination\AbstractPaginator)
{{$products->links()}}
#endif
You need to check wheater the $products is instance of Illuminate\Pagination\AbstractPaginator. It can be an array or Laravel's Collection as well.
As of laravel 7 you can now do this:
#if( $vehicles->hasPages() )
{{ $vehicles->links() }}
#endif
The beautiful way:
#if ($products->hasMorePages())
{{ $products->links() }}
#endif
Click here to see the official documentation
Don't use a check on the base class of the variable. This could lead to problems with changing base classes in future Laravel versions. Simply check whether the method links exists:
#if(method_exists($products, 'links'))
{{ $products->links() }}
#endif
Corrected code (add "isset"):
#if(isset($products->links()))
{{$products->links()}}
#endif
Shorter version:
{{$products->links() ?? ''}}
It will work for paginate, simplePaginate and when there is no pagination. The solution with "$products->hasMorePages()" will not display pagination links on the last page.
Another way:
#if (class_basename($products) !== 'Collection')
{{ $products->links() }}
#endif
You can use PHP function: get_class($products) - to get full class name.
Laravel should have some function to check ->paginate() is in use.
just write paginate(0) instead of get()
Blade Template: simply use {{$products->links}}. no #if #endif needed.
laravel paginate have 2 type :
simplePaginate() will return \Illuminate\Pagination\Paginator
paginate() will return Illuminate\Pagination\LengthAwarePaginator
Based on the above conditions, you can try this solution :
#if(
$products instanceof \Illuminate\Pagination\Paginator ||
$products instanceof Illuminate\Pagination\LengthAwarePaginator
)
{{ $products->links() }}
#endif
Juse use below format
#if($products instanceof \Illuminate\Pagination\LengthAwarePaginator )
{{$products->links()}}
#endif
#if($products->currentPage() > 1)
{{$products->links()}}
#endif
I am passing some data in Laravel template. I am using if condition in template but my else part is not working my condition is #if($portfolio->active) class="label label-success" #else class="label label-danger" #endif. I checked {{$portfolio->active}} but it has value 0 so my else part should be run but always if part is running my else part is not working
#if($portfolio->active ==0)
#else
#endif
Write this in your Portfolio model:
protected $casts = ['active' => 'boolean'];
This will make the 'active' field into a boolean. Then you can write your if else as:
#if($portfolio->active)
#else
#endif
This will make your code a little bit more readable. You can avoid the else by doing the converse like:
#if(!portfolio->active)
//your else code here
#endif
//your if code here
You can also use in this way
class="{{ ($portfolio->active) ? 'label label-success' : 'label label-danger' }}
I am trying to render my javascript codes using blade template in Laravel 4.2 and unable to render inside #elseif but is working in #if statement. Can anyone verify this is a flaw in Laravel 4.2 or it is an error.
#if ( $page == 'loginpage' )
#yield('body')
{{ HTML::script('js/jquery.min.js') }} //works here
#elseif ( $page == 'resetpage' )
#include('panel')
#yield ('body')
#section('js')
{{ HTML::script('js/jquery.min.js') }} //cannot render from here neither from the yielded section.
#show
#endif
Only the HTML::script in #if statement works. Any help is granted. Thanks in advance.
Edit:
Solved the problem as i have overrided the section with some other sections. Sorry to bother you all.