How to create a pagination layout in Laravel 9 using Bootstrap 5 like this style.
pagination layout
Hello I try to answer your question. In laravel 9, maybe you can try this code and implement to your project.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Paginator::useBootstrap();
}
Or you can see full tutorial in this link Laravel 9 Pagination Example with Bootstrap Tutorial
Thank you
You can publish the pagination views. after that, you can customize it
php artisan vendor:publish --tag=laravel-pagination
resources/views/vendor/pagination/default.blade.php
#if ($paginator->hasPages())
<nav>
<ul class="pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled" aria-disabled="true" aria-label="#lang('pagination.previous')">
<span aria-hidden="true">‹</span>
</li>
#else
<li>
‹
</li>
#endif
{{-- Pagination Elements --}}
#foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
#if (is_string($element))
<li class="disabled" aria-disabled="true"><span>{{ $element }}</span></li>
#endif
{{-- Array Of Links --}}
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="active" aria-current="page"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>
›
</li>
#else
<li class="disabled" aria-disabled="true" aria-label="#lang('pagination.next')">
<span aria-hidden="true">›</span>
</li>
#endif
</ul>
</nav>
#endif
Related
I'm building an application where some page needs pagination. I already made a design for my application's pagination. But how can I use that with laravel paginate?
I did this for paginate in the controller
$products= Products::paginate(9);
In my blade, this is my desired HTML for pagination
<div class="row pt-3 pb-5">
<div class="col-12 d-flex justify-content-end">
<div class="pagination-btn-box">
<button class="active-pagination-btn">1</button>
<button>2</button>
<button>3</button>
<button>4</button>
<button>5</button>
<button>...</button>
<button>Next</button>
</div>
</div>
</div>
Now, How can I implement this with laravel for paginating?
In controller return view
return view('blade file name',compact('products'))
in blade do like below
<div class="container">
#foreach ($products as $product)
{{ $product->fieldname}}
#endforeach
</div>
{{ $products->links() }}
$products->links() will generate default pagination.
if you want to customize pagination template then run following command
php artisan vendor:publish --tag=laravel-pagination
this will generate folder in views/vendor/pagination
default.blad.php look like below
#if ($paginator->hasPages())
<nav>
<ul class="pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled" aria-disabled="true" aria-label="#lang('pagination.previous')">
<span aria-hidden="true">‹</span>
</li>
#else
<li>
‹
</li>
#endif
{{-- Pagination Elements --}}
#foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
#if (is_string($element))
<li class="disabled" aria-disabled="true"><span>{{ $element }}</span></li>
#endif
{{-- Array Of Links --}}
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="active" aria-current="page"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>
›
</li>
#else
<li class="disabled" aria-disabled="true" aria-label="#lang('pagination.next')">
<span aria-hidden="true">›</span>
</li>
#endif
</ul>
</nav>
#endif
Still if you want to use your own pagination view then its possible .First you need to register your custom view in Service Provider
<?php
namespace App\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//defaultStringLength changed to 191 to support mysql below 5.7
Schema::defaultStringLength(191);
Paginator::defaultView('custom-pagination');
Paginator::defaultSimpleView('simple-pagination');
}
}
so here in my example i have created two blade template called custom-pagination.blade.php and simple-pagination.blade.php
Ref:https://laravel.com/docs/8.x/pagination#displaying-pagination-results
This is the blade view to create the url links
#section ('category')
<ul class="main-categories">
#foreach($category as $category)
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#endforeach
</ul>
#endsection
when i click for the first time it's working fine and the url I get is
http://localhost/shopnew/public/glassfilm/Frosted
when I click for the secon time the url I get is
http://localhost/shopnew/public/glassfilm/glassfilm/Frosted
what I am doing wrong?
Change your route as below :
#section ('category')
<li class="main-nav-list">
<a href="{{ route('glassfilm-category',['name' => $category->category]) }}"> {{ $category->category }}
<span class="number">{{ $category->count}}</span>
</a>
</li>
#endsection
asset() method is used to include CSS/JavaScript/images files.
url() method used to generate a URL to a link.
Laravel's route() method is very helpful. So you can try:
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#section ('category')
<ul class="main-categories">
#foreach($category as $category)
<li class="main-nav-list">{{ $category->category }}<span class="number">{{ $category->count}}</span></li>
#endforeach
</ul>
#endsection
How do I include snippets, or partials in pages in Laravel 5? In node/angular, it's quite easy to simply load different modules and such on a page.
For example, on my home page, I'm looping through some data:
<h1>Home</h1>
#if (count($practice))
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
#endif
If I include the login snippet on the page, it covers up the rest of the data:
<h1>Home</h1>
#if (count($practice))
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
#endif
#include('auth.login')
Try to include it in a new row :
<h1>Home</h1>
#if (count($practice))
<div class="row">
<div class="col-md-12">
<ul>
#foreach($practice as $val)
<li>{{ $val }}</li>
#endforeach
</ul>
</div>
</div>
#endif
<div class="row">
<div class="col-md-12">
#include('auth.login')
</div>
</div>
I have a list of projects, where I can click on and it (needs to) show the project name, and the list of the projects' tasks (ToDo application)
But when I click on a certain project I get this error.
(The "H2" project name will show up)
h2>{{{ $project->name }}}</h2>
#if ( !$project->tasks->count())
There are no tasks for this project.
#else
<ul>
#foreach( $project->tasks as $task)
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
$project->tasks is an array and you cant call count() on the array. So try with -
count($project->tasks)
Try count($project->tasks)==0
h2>{{{ $project->name }}}</h2>
#if ( count($project->tasks)==0)
There are no tasks for this project.
#else
<ul>
#foreach( $project->tasks as $task)
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
I see your issue is resolved but this might help someone else :D
The following piece of code might work for you, but it's not that we can't call count() on the array.
count($project->tasks)
Instead of this
h2>{{{ $project->name }}}</h2>
#if ( !$project->tasks->count()) // tasks is plural -> wrong.
There are no tasks for this project.
#else
<ul>
#foreach( $project->tasks as $task) // here too!
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
Try doing this
h2>{{{ $project->name }}}</h2>
#if ( !$project->task->count())
There are no tasks for this project.
#else
<ul>
#foreach( $project->task as $task)
<li>{{ $task->name }}</li>
#endforeach
</ul>
#endif
I use collection for empty objects this could work better
in my controller
if ($search) {
$taks = MODEL::search($search)->get();
} else {
$tasks = collect([]);
}
in my view
#if ($tasks->count())
<ul class="list-group">
#foreach($tasks as $task)
<li>info and action</li>
#endforeach
</ul>
#else
<p class="text-danger">Error message</p>
#endif
I have navigation bar that I'd like to show a link to the Admin Dashboard if the user logged in is an admin. If not, it should display nothing. I have something similar set up with guests e.g.
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
{{ Auth::user()->name }} <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<li>Profile</li>
<li>Logout</li>
</ul>
</li>
#endif
But how can I do this for a logged in user and an admin? I currently have
<ul class="nav navbar-nav">
<li>Home</li>
#if (Auth::guest())
#else
<li>Admin Dashboard</li>
#endif
</ul>
I have middleware set up on the admin route like so
Route::get('admin', ['middleware' => 'admin', 'uses' => 'AdminController#index']);
Which looks like
public function handle($request, Closure $next)
{
if ($request->user()->role != 1)
{
return redirect('home');
}
return $next($request);
}
And that's fine, I just don't know how to get it to define a section of a blade template.
Looks like your user model has an attribute named role, so you can do something like this:
<ul class="nav navbar-nav">
<li>Home</li>
#if (Auth::user()->role != 1)
{{-- I am not an admin user --}}
#else
{{-- I am an admin user --}}
#endif
</ul>
If it is not your case, then you need add a new attribute to the user model. By that way you are able emulate the code above.
For a better code structure and order, I suggest to you make a fuction inside of user model like this:
public function isAdmin(){
return (\Auth::check() && $this->role == 1);
}
or another one to check if it is a regular user:
/** An user who is authenticated but it is not an admin */
public function isRegular(){
return (\Auth::check() && $this->role != 1);
}
Then, in you application and views you can use them like:
#if (Auth::user()->isRegular())
{{-- I am not an admin user --}}
#else
{{-- I am an admin user --}}
#endif
Or
#if (Auth::user()->isAdmin())
{{-- I am an admin user --}}
#else
{{-- I am not an admin user --}}
#endif