Blade: How to include default view If include view error? - laravel

I want to #include('item_1'), if not the item_1 view then #include('default').
Idk how to include default view.
My laravel version is 5.2.

You can try exists() method:
#include(View::exists('item_1') ? 'item_1' : 'default')

#if (View::exists('item_1'))
#include('item_1')
#else
#include('default')
Just use a if statement

Related

How can I check if collection empty in view blade laravel?

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

Laravel hide code if requested Path was

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

"Request::is()" not working on laravel 5.5, in blade

I am trying to set active classes on active menus. In the past I was using Request::is() function for this, but in the new version of laravel it says "Class 'Request' not found."
As you are using blade you can use request helper method. Try like this
request()->is('your_url');
do not put / before the route.
it should be like
#if(Request::is('index'))
#include('include.showcase')
#endif
please check in your code
you add this line as namespace or not
use Illuminate\Http\Request;
and you can also pass one varriable with view from controller side and check on blade template if you get that data then you can use active class for that menu. like this
in controller what i had used in my project
$data['page-tab'] = 'adduser';
return view('user.add_user',compact('data'));
and in blade file i had checked this like this in sidebar section
<li class="{{ isset($data['page-tab']) && $data['page-tab'] == 'adduser' ? 'active' : '' }}"><i class="fa fa-file-text-o"></i>Manage Member</li>
hope this works for you.
Use it without the '/'
like this:
#Request::is('home')
not
#request::is('/home')

How to check if used paginate in laravel

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

Laravel Pagination links not including other GET parameters

I am using Eloquent together with Laravel 4's Pagination class.
Problem: When there are some GET parameters in the URL, eg: http://site.example/users?gender=female&body=hot, the pagination links produced only contain the page parameter and nothing else.
Blade Template
{{ $users->link() }}
There's a ->append() function for this, but when we don't know how many of the GET parameters are there, how can we use append() to include the other GET parameters in the paginated links without a whole chunk of if code messing up our blade template?
I think you should use this code in Laravel version 5+.
Also this will work not only with parameter page but also with any other parameter(s):
$users->appends(request()->input())->links();
Personally, I try to avoid using Facades as much as I can. Using global helper functions is less code and much elegant.
UPDATE:
Do not use Input Facade as it is deprecated in Laravel v6+
EDIT: Connor's comment with Mehdi's answer are required to make this work. Thanks to both for their clarifications.
->appends() can accept an array as a parameter, you could pass Input::except('page'), that should do the trick.
Example:
return view('manage/users', [
'users' => $users->appends(Input::except('page'))
]);
You could use
->appends(request()->query())
Example in the Controller:
$users = User::search()->order()->with('type:id,name')
->paginate(30)
->appends(request()->query());
return view('users.index', compact('users'));
Example in the View:
{{ $users->appends(request()->query())->links() }}
Be aware of the Input::all() , it will Include the previous ?page= values again and again in each page you open !
for example if you are in ?page=1 and you open the next page, it will open ?page=1&page=2 So the last value page takes will be the page you see ! not the page you want to see
Solution : use Input::except(array('page'))
Laravel 7.x and above has added new method to paginator:
->withQueryString()
So you can use it like:
{{ $users->withQueryString()->links() }}
For laravel below 7.x use:
{{ $users->appends(request()->query())->links() }}
Not append() but appends()
So, right answer is:
{!! $records->appends(Input::except('page'))->links() !!}
LARAVEL 5
The view must contain something like:
{!! $myItems->appends(Input::except('page'))->render() !!}
Use this construction, to keep all input params but page
{!! $myItems->appends(Request::capture()->except('page'))->render() !!}
Why?
1) you strip down everything that added to request like that
$request->request->add(['variable' => 123]);
2) you don't need $request as input parameter for the function
3) you are excluding "page"
PS) and it works for Laravel 5.1
In Your controller after pagination add withQueryString() like below
$post = Post::paginate(10)->withQueryString();
Include This In Your View
Page
$users->appends(Input::except('page'))
for who one in laravel 5 or greater
in blade:
{{ $table->appends(['id' => $something ])->links() }}
you can get the passed item with
$passed_item=$request->id;
test it with
dd($passed_item);
you must get $something value
In Laravel 7.x you can use it like this:
{{ $results->withQueryString()->links() }}
Pass the page number for pagination as well. Some thing like this
$currentPg = Input::get('page') ? Input::get('page') : '1';
$boards = Cache::remember('boards' . $currentPg, 60, function() {
return WhatEverModel::paginate(15);
});
Many solution here mention using Input...
Input has been removed in Laravel 6, 7, 8
Use Request instead.
Here's the blade statement that worked in my Laravel 8 project:
{{$data->appends(Request::except('page'))->links()}}
Where $data is the PHP object containing the paginated data.
Thanks to Alexandre Danault who pointed this out in this comment.

Resources