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
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
If I dd($items); in the controller, the result like this :
In the view blade laravel I check if collection empty like this :
#if($items)
...
#endif
But it does not work
How can I solve this problem?
You could use $items->isEmpty(); or $items->isNotEmpty();
Like so:
#if(!$items->isNotEmpty())
...
#endif
You can further read over here:
https://laravel.com/docs/5.5/collections#method-isempty
i normally double check so that if i don't get the value the page keeps running :
#if(isset($items))
#if(!empty($items))
.....
#endif
#endif
hope it helps
#if ( $items->count() )
....
#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
In blade, If we want to check that the current route matches with a route or not, we can simply use:
#if(Route::currentRouteName() == 'parameter')
{{ 'yes' }}
#else
{{ 'no' }}
#endif
But what if we want to match it with a wildcard like:
#if(Route::currentRouteName() == 'parameter.*')
{{ 'yes' }}
#else
{{ 'no' }}
#endif
Is there any solution for that?
I have tried "*" and ":any", but it didn't work.
Note: I want to check route, not URL.
Any help would be appreciated.
Thanks,
Parth Vora
Use Laravel's string helper function
str_is('parameter*', Route::currentRouteName())
It'll return true for any string that starts with parameter
I had the same problem. I wanted to toggle an active class based on a URI.
In blade (Laravel 6x), I did:
(request()->is('projects/*')) ? 'active' : ''
You can also make use of Blades Custom If Statements and write something like this in your AppServiceProvider.php:
public function boot()
{
Blade::if('route', function ($route) {
return Str::is($route, Route::currentRouteName());
});
}
then you can use it in a blade view like this:
<li #route('admin.users*') class="active" #endroute>
Users
</li>
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.