How to customize default pagination in laravel 8? - php

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

Related

pagination layout in Laravel use Bootstrap

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

How to show weblog categories in foreach in laravel?

I have a Category and Weblog for my website.
This is Category Model :
public function weblogs()
{
return $this->belongsToMany(Weblog::class);
}
And this is Weblog Model :
public function categories()
{
return $this->belongsToMany(Category::class);
}
As you can see there is a realation between Weblog and Category.
This is Controller :
$weblogs = Weblog::paginate(9);
return view('index', compact('weblogs'));
And this is my blade for showing weblog items :
#foreach($weblogs as $weblog)
<div class="col-lg-4 col-sm-6">
<div class="blog-item">
<div class="thumbnail">
<a href="#"><img alt=""
src="/Weblog/image/{{ $weblog->image }}"></a>
</div>
<h4>{{ $weblog->name }}</h4>
<ul>
<li>{{ jdate($weblog->created_at)->format('%d %B %Y') }}</li>
<li>سبک زندگی</li>
</ul>
<div class="blog-btn">
بیشتر بخوانید
</div>
</div>
</div>
#endforeach
I want to show category belong to any item of weblog, how can i do this ?
Just access property in your template
<div class="blog-item">
...
#foreach ($weblog->categories as $category)
{{ $category }}
#endforeach
...
</div>
You can also load relations earlier
$weblogs = Weblogs::with('categories')->paginate(9)
You can read more about it here https://laravel.com/docs/7.x/eloquent-relationships#relationship-methods-vs-dynamic-properties and here https://laravel.com/docs/7.x/eloquent-relationships#eager-loading

laravel breadcrumbs not rendering html in Laravel 5.4

Within a Layout
#section('breadcrumbs', Breadcrumbs::render('messages'))
#section('content')
#include('layouts.breadcrumbs')
breadcrumbs.blade.php
<div class="fluid-container">
<div class="container">
<div class="row">
<div class="col-md-12">
#yield('breadcrumbs')
</div>
</div>
</div>
</div>
Standard BS3 view with DaveJamesMillar Breadcrumbs
#if ($breadcrumbs)
<ol class="breadcrumb">
#foreach ($breadcrumbs as $breadcrumb)
#if ($breadcrumb->url && !$breadcrumb->last)
<li>{{ $breadcrumb->title }}</li>
#else
<li class="active">{{ $breadcrumb->title }}</li>
#endif
#endforeach
</ol>
#endif
Appeared to be working fine until upgrading to L5.4, now rather than displaying the breadcrumbs it displays non-processed HTML
<ol class="breadcrumb"> <li>Home</li> <li> class="active">Messages</li></ol>
After reading the latest docs for davejamesmillar laravel-breadcrumbs with support for L5.4 https://media.readthedocs.org/pdf/laravel-breadcrumbs/latest/laravel-breadcrumbs.pdf with reference to 1.4.2 using Blade Sections nothing appears to have changed in the way this needs to be coded. Unsure why the HTML is not being processed to display as a link.
RAR, hours later! Appears Laravel 5.4 runs a htmlentities when injecting a variable into a #section
I changed
#section('breadcrumbs', Breadcrumbs::render('messages'))
to
#section('breadcrumbs') {!! Breadcrumbs::Render('messages') !!} #endsection
And the html is now being processed and displayed as it should.

Adding in code snippets/partials on a page

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>

How can I use Middleware to define a section of a blade template?

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

Categories