Laravel Pagination "Three Dots" Separator customization - php

Im currently using Laravel 5.3 and was wondering if there is a option for the customization of the Three Dots deperator. (skips page 9-10, which is to late)
Example
Currently the Three dots initiate if there are more than 11 pages... Which isnt quiet useful if your site is responsive. if there are to many pages so it breaks into 2 lines.
Example2
I cannot find anything regarding there being options for $resource->links(). But if there is please tell me! Much appreciated.
Edit: it has to do with the following function:
vendor/laravel/framework/src/Illuminate/Pagination/LengthAwarePaginator.php (page: 128, render()). The current function does not support a second variable. So i guess i have to rebuild it?

This is a solution for Laravel 5.5+. Here is what it does:
Shows the first and the last page.
Shows previous and next two pages from the current page.
Three dots only appear on the left after the current page is greater than 4.
Three dots only appear on the right after the current page is less than the 4 - (count of pages).
<!-- Pagination Elements -->
#foreach ($elements as $element)
<!-- Array Of Links -->
#foreach ($element as $page => $url)
<!-- Use three dots when current page is greater than 4. -->
#if ($paginator->currentPage() > 4 && $page === 2)
<li class="page-item disabled"><span class="page-link">...</span></li>
#endif
<!-- Show active page else show the first and last two pages from current page. -->
#if ($page == $paginator->currentPage())
<li class="page-item active"><span class="page-link">{{ $page }}</span></li>
#elseif ($page === $paginator->currentPage() + 1 || $page === $paginator->currentPage() + 2 || $page === $paginator->currentPage() - 1 || $page === $paginator->currentPage() - 2 || $page === $paginator->lastPage() || $page === 1)
<li class="page-item"><a class="page-link" href="{{ $url }}">{{ $page }}</a></li>
#endif
<!-- Use three dots when current page is away from end. -->
#if ($paginator->currentPage() < $paginator->lastPage() - 3 && $page === $paginator->lastPage() - 1)
<li class="page-item disabled"><span class="page-link">...</span></li>
#endif
#endforeach
#endforeach
Output:
Page 1 (first page)
Page 3
Page 10 (last page)

Option 1 :
You can customize default files but don't change vendor files directly. Publish them and then add modifications to that.
php artisan vendor:publish --tag=laravel-pagination
This command will automatically create the folder /resources/views/vendor/pagination and you have your files for modification.
You can get more information here : laravel pagination
Option 2:
Maybe you want to get rid of the files that are generated by default. Or, perhaps you want to assign another file to be responsible for your default pagination view.
All of this is possible, but you will need to inform the AppServiceProvider for this action by calling the new pagination views in the boot() method:
use Illuminate\Pagination\Paginator;
public function boot(){
Paginator::defaultView('your-pagination-view-file-name');
Paginator::defaultSimpleView('your-pagination-view-file-name');
}
Get information for defaultView and defaultSimpleView here :laravel pagination
I have created new file for pagination and added in AppServiceProvider.
#if ($paginator->hasPages())
<ul class="blog-pagenation">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled"><a>«</a></li>
#else
<li>«</li>
#endif
#if($paginator->currentPage() > 3)
<li class="hidden-xs">1</li>
#endif
#if($paginator->currentPage() > 4)
<li><a>...</a></li>
#endif
#foreach(range(1, $paginator->lastPage()) as $i)
#if($i >= $paginator->currentPage() - 2 && $i <= $paginator->currentPage() + 2)
#if ($i == $paginator->currentPage())
<li class="active"><a class="active">{{ $i }}</a></li>
#else
<li>{{ $i }}</li>
#endif
#endif
#endforeach
#if($paginator->currentPage() < $paginator->lastPage() - 3)
<li><a>...</a></li>
#endif
#if($paginator->currentPage() < $paginator->lastPage() - 2)
<li class="hidden-xs">{{ $paginator->lastPage() }}</li>
#endif
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>»</li>
#else
<li class="disabled"><a>»</a></li>
#endif
</ul>
#endif
By using this i am able to get 3 dots in starting and ending you have to customize classes based on your themes.

Adding to the previous response, once you generate the vendor view files with the artisan command php artisan vendor:publish you can create a new one in that folder and call it for example custom.blade.php and put the following code:
#if ($paginator->hasPages())
<ul class="custom-pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled pageNumber"><span>« Prev</span></li>
#else
<li><a class="pageNumber" href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a></li>
#endif
{{-- Pagination Elements --}}
#foreach ($elements as $element)
{{-- Array Of Links --}}
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page === $paginator->currentPage())
<li class="active pageNumber"><span>{{ $page }}</span></li>
#elseif (($page === $paginator->currentPage() + 1 || $page === $paginator->currentPage() + 2)
|| $page === $paginator->lastPage())
<li><a class="pageNumber" href="{{ $url }}">{{ $page }}</a></li>
#elseif ($page === $paginator->lastPage()-1)
<li class="disabled"><span>...</span></li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li><a class="pageNumber" href="{{ $paginator->nextPageUrl() }}" rel="next">Next »</a></li>
#else
<li class="disabled pageNumber"><span>Next »</span></li>
#endif
</ul>#endif
The important part of the code for the three dots is in the {{-- Array Of Links --}} portion. I think this more or less does what you need but may require additional tweaking.
then you can use it in your view like this:
<div class="pagination">
#if ($users instanceof \Illuminate\Pagination\LengthAwarePaginator)
{{ $users->links('vendor.pagination.custom') }}
#endif
</div>

Related

Laravel blade on VSCode indention problem

Every time I do a format shortcut (Alt + Shift + F) for my .blade documents, codes that start with # do not indent the way I want them to be. Below is the sample code of an unformatted basic if/else + foreach on a .blade file.
// unformatted codeblock
<h1 class="poppins-medium text-xl text-darkergreen text-center">
#if (Auth::user()->role == 'auditor' || Auth::user()->role == 'accreditor')
#foreach ($documentGalleryTitle as $record)
#if ((Str::lower(Str::snake(Str::replaceLast(' Taskforce', '', $record->name)))) == $folder_name)
{{ Str::replaceLast(' Taskforce', '', $record->name) . ' Documents'}}
#endif
#endforeach
#elseif (Auth::user()->role == 'taskforce')
{{ $userRecord->nonOfficialsCategories->area_number . Auth::user()->subcategory . " Documents" }}
#endif
</h1>
As you can see on the previous codeblock, codes inside the parent #if/else should be indented. Same with the codes inside the #foreach, and the #if/endif codeblock.
I want the codeblock to look like the code below rather than the previous code above:
// how it should be
<h1 class="poppins-medium text-xl text-darkergreen text-center">
#if (Auth::user()->role == 'auditor' || Auth::user()->role == 'accreditor')
#foreach ($documentGalleryTitle as $record)
#if ((Str::lower(Str::snake(Str::replaceLast(' Taskforce', '', $record->name)))) == $folder_name)
{{ Str::replaceLast(' Taskforce', '', $record->name) . ' Documents'}}
#endif
#endforeach
#elseif (Auth::user()->role == 'taskforce')
{{ $userRecord->nonOfficialsCategories->area_number . Auth::user()->subcategory . " Documents" }}
#endif
</h1>
Is there a particular extension that I need to install + edit to create my desired format?

how to restrict access to certain parts of a view laravel

i want to restrict certain parts of a view if a user is not a guest/admin is not supposed to see.the application has 3 users admin,vendor,customer
#if (Auth::check() && !Auth::user()->role == 'customer')
<div class="collapse show" id="users-nav">
<ul class="nav nav-sm flex-column">
<li class="nav-item">
<a class="nav-link" href="{{ route('profile.edit') }}">
{{ __('User Profile') }}
</a>
</li>
am getting the error
The vendor doesnt see too that part and i want him to see together with admin
You should change your if clause from this:
#if (Auth::check() && !Auth::user()->role == 'customer')
To this:
#if (Auth::check() && !(Auth::user()->role == 'customer'))
Or to this (as pointed by #miken32), easier to read:
#if (Auth::check() && (Auth::user()->role !== 'customer'))
The first one is always false, pay attention to round brackets.
Blade provides several shortcuts for checking if a user is signed in. for example:
#auth
<p>Only logged in users can view this</p>
Logout
#endauth
#guest
<p>Only non-logged in users can view this</p>
Login
#endguest
You can view additional blade templating if statements here: https://laravel.com/docs/5.8/blade#if-statements

How to make one submenu active in laravel blade?

Here I want to make one submenu active out of four submenu. This is for dynamic slug .
<div class="solution_tabs">
#foreach($allMenu as $menu)
<ul class="submenu">
#if(isset($menu->submenus))
#foreach($menu->submenus as $submenu)
#if(isset($submenu->page->slug))
<li class="active">{{ $submenu->name}}</li>
#else
<li>{{ $submenu->name}}</li>
#endif
#endforeach
#endif
</ul>
#endforeach
</div>
same as this image,i want one active submenu from dynamic submeun of the menu
You should compare the current URL with your link URLs in order to detect coincidences. One way to accomplish it could be injecting request in your blade file, using
#inject('request', 'Illuminate\Http\Request')
and then check for matches like this:
<li class="{{ $request->segment(1) == $submenu->page->slug ? 'active' : '' }}">{{ $submenu->name}}</li>
Please note that segment() is 0 based, so segment(1) works when your url is like example.com/segment(0)/page->slug

Refactor to show link or not in blade

I felt this code look a bit messy, the logic is to show a link <a href or otherwise show text only.
How can I refactor this to look cleaner and maintainable?
<ol class="breadcrumb">
<li class="{{ $active == 'sign_in'? 'active':'' }}">
#if($active != 'sign_in')
#php($showLink = true)
#else
#php($showLink = false)
#endif
#if($showLink)
<a href="{{ url_secure('sign_in') }}">
#endif
Sign In
#if($showLink)
</a>
#endif
</li>
<li class="{{ $active == 'article'? 'active':'' }}">
#if($active != 'article' && $showLink)
#php($showLink= true)
#else
#php($showLink= false)
#endif
#if($showLink)
<a href="{{ url_secure('article')}}">
#endif
Articles
#if($showLink)</a>#endif
</li>
<li> </li> //repeat the code logic like above
</ol>
It would be good if there a way to reduce if conditions and use loop.
Why not just:
<li class="{{ $active == 'sign_in'? 'active':'' }}">
#if ($active != 'sign_in')
Sign In
#else
Sign In
#endif
</li>
Also it is unclear why you check $showLink that was set previoulsy.
Okay, as I've read your comments, I've modified template a bit with using foreach loop:
#php
// some initial variables
$allowed_to_click = true;
// this is a link to selected page
$selected_link = 'something';
#endphp
#foreach ($links as $link)
#php
// check if this link is ACTIVE
$active = $link == $selected_link;
// check if we can CLICK this link
$allowed_to_click = $active || $allowed_to_click;
#endphp
<li class="{{ $active ? 'active':'' }}">
#if ($allowed_to_click)
Some Caption
#else
Some Caption
#endif
</li>
#php
// if link is ACTIVE - following links are unclickable
if ($active) {
$allowed_to_click = false;
}
#endphp
#endforeach

Dealing with breadcrumbs in Laravel 4

I have simple breadcrumbs on my page
<ul class="breadcrumb">
<li>
<i class="glyphicon glyphicon-home"></i>
Home
</li>
<?php $link = URL::to('/'); ?>
#for($i = 1; $i <= count(Request::segments()); $i++)
<li>
#if($i < count(Request::segments()) & $i > 0)
<?php $link .= "/" . Request::segment($i); ?>
{{Request::segment($i)}}
#else {{Request::segment($i)}}
#endif
</li>
#endfor
</ul>
This produce breadcrumbs like Home / Page / etc
The problem here is that I have views in my router is like this
Route::get('/users/profile', ['uses' => 'UsersController#viewProfile', 'before' => 'auth|csrf']);
Route::get ('/admin/pages/edit/{pageId}', ['uses' => 'AdminController#pageEdit', 'before' => 'admin']);
So for both routes breadcrumbs will be
Home / Users / Profile
Home / Admin / Pages / Edit / 1
Here middle path in breadcrumb is not existing in this case / Users / and / Edit / .. There are a lot pages like this. Is there a way to avoid this?
Edit:
I know I just can change routes in my router but I don't want. So need some other way to achieve this..
You can use this, and then add a condition (line 24) that:
#if ($title != 'Users' && $title != 'Edit')
<span>{{ $title }}</span>
#endif
Alternatively, if you have lots of these values that you would like to avoid, then you can write an array and check them with the same approach.

Categories