How to implement onEachSide(1) in laravel 9 with bootstrap 5? - php

I'm trying to implement onEachSide(1) into custom pagination in bootstrap 5, but can't understand how should I implement this.
Below is my code reference.
DistributorController.php
public function distributorList(Request $request)
{
$input = $request->all();
$search = isset($input['search']) ? $input['search'] : '';
$list = $this->distributorRepository->allList($input);
return view('admin.distributor.distributor')->with(['data'=>$list,'filter' => $search]);
}
allList method inside distributorRepository.php
public function allList($input)
{
$res = $this->makeModel();
if (isset($input['search'])) {
$search = $input['search'];
$res = $res->where(function($query) use($search) {
$query->where('distributorId', 'LIKE', "%{$search}%" )
->orWhere ( 'name', 'LIKE', "%{$search}%" )
->orWhere ( 'mobile_no', 'LIKE', "%{$search}%" )
->orWhere ( 'email', 'LIKE', "%{$search}%" );
});
}
$res = $res->paginate(1)->withQueryString();
return $res;
}
custom-pagination.blade.php
#if ($paginator->hasPages())
<div class="position-absolute start-0">
<select class="select2 px-2 py-2" name="limit" id="limit">
<option value="15">15</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="500">500</option>
<option value="all">All</option>
</select>
</div>
<div class="position-absolute end-0 d-flex">
<div class="px-2 py-2">
Showing {{ $paginator->firstItem() }}
to {{ $paginator->lastItem() }}
of {{ $paginator->total() }} results
</div>
<nav>
<ul class="pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="page-item disabled" aria-disabled="true" aria-label="#lang('pagination.previous')">
<a class="page-link" href="#" tabindex="-1">Previous</a>
</li>
#else
<li class="page-item">
<a class="page-link" href="{{ $paginator->previousPageUrl() }}" rel="prev"
aria-label="#lang('pagination.previous')">Previous</a>
</li>
#endif
{{-- Pagination Elements --}}
#foreach ($elements as $element)
{{-- "Three Dots" Separator --}}
#if (is_string($element))
<li class="page-item 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="page-item active" aria-current="page">
<a class="page-link" href="{{ $url }}">{{ $page }}
<span class="sr-only">(current)</span>
</a>
</li>
#else
<li class="page-item">
<a class="page-link" href="{{ $url }}">{{ $page }}</a>
</li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li class="page-item">
<a class="page-link" href="{{ $paginator->nextPageUrl() }}" rel="next"
aria-label="#lang('pagination.next')">Next</a>
</li>
#else
<li class="page-item disabled" aria-disabled="true" aria-label="#lang('pagination.next')">
<a class="page-link" href="#">Next</a>
</li>
#endif
</ul>
</nav>
</div>
#endif
But i want like this :-
Previous 1 2 ... 9 10 11 ... 19 20 Next

im new for laravel but i think you can use this
#if ($paginator->hasPages())
<ul class="pagination pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled"><span>«</span></li>
#else
<li>«</li>
#endif
#if($paginator->currentPage() > 3)
<li class="hidden-xs">1</li>
#endif
#if($paginator->currentPage() > 4)
<li><span>...</span></li>
#endif
#foreach(range(1, $paginator->lastPage()) as $i)
#if($i >= $paginator->currentPage() - 1 && $i <= $paginator->currentPage() + 1)
#if ($i == $paginator->currentPage())
<li class="active"><span>{{ $i }}</span></li>
#else
<li>{{ $i }}</li>
#endif
#endif
#endforeach
#if($paginator->currentPage() < $paginator->lastPage() - 3)
<li><span>...</span></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"><span>»</span></li>
#endif
</ul>#endif

onEachSide just sets the value to be used when creating pages, so you can modify the value before you pass the results to the view.
$res = $res->paginate(1)->withQueryString();
$res->onEachSide(1);

Related

How to create url form in menu dynamic laravel

I just want to know how create condition in laravel if url field / form in menus is true then in menu href will linked into $url, unless if url form is null, then in menu href will linked into $id just as usual.
my blade menu child :
<ul>
#foreach ($childs as $child)
<li>#if (count($child->childs)) #endif</li>
<li class="dropdown">
{{$child->nama}}#if (count($child->childs)) #endif
#if (count($child->childs))
#include('menuChild', ['childs'=> $child->childs])
#endif
</li>
#endforeach
</ul>
Try to change your code into this
<ul>
#foreach ($childs as $child)
<li><a #if (request($child->link, true))href="{{$child->link}}"#endif></a></li>
{{-- <li> #if (count($child->childs)) #endif</li> --}}
<li class="dropdown">
{{$child->nama}}#if (count($child->childs)) #endif
{{-- <li class="dropdown">#if (count($child->childs)) #endif --}}
#if (count($child->childs))
#include('menuChild', ['childs'=> $child->childs])
#endif
{{-- </li> --}}
</li>
#endforeach
</ul>

laravel php bootstrap nested tabs not workinfg

I´m having trouble with dynamic tabs, php code works fine. The problem is with bootstrap tabs.
Already tried updating bootstrap files and it did't work!
<ul class="nav nav-tabs nav-justified tabprofile" id="outerTab" role="tablist">
#foreach($parentCategories as $count => $category)
<li class="nav-item">
<a
#if($count == 0) class="nav-link fade show active " #else class="nav-link checklist" #endif
id="{{$category->id}}-tab" data-toggle="tab" href="#{{$category->id}}"
role="tab" aria-controls="{{$category->id}}" #if($count == 0) aria-selected="true" #else aria-selected="false" #endif>{{$category->name}}</a>
</li>
#endforeach
</ul>
<div class="tab-content">
#foreach($parentCategories as $count => $category)
<div #if($count == 0) class="tab-pane fade show active" #else class="tab-pane checklist" #endif id="{{$category->id}}" role="tabpanel" aria-labelledby="{{$category->id}}-tab" >
<ul class="nav nav-tabs nav-justified" id="innerTab" role="tablist">
#foreach($category->children as $count => $sub)
<li class="nav-item">
<a class="nav-link #if($count == 0) show active #endif" id="{{$sub->id}}-tab" data-toggle="tab" href="#{{$sub->id}}"
role="tab" aria-controls="{{$sub->id}}" #if($count == 0) aria-selected="true" #else aria-selected="false" #endif>{{$sub->slug}}</a>
</li>
#endforeach
</ul>
<div class="tab-content">
#foreach($category->children as $count => $sub)
<div #if($count == 0) class="tab-pane fade show active checklist" #else class="tab-pane checklist" #endif id="{{$sub->id}}" role="tabpanel" aria-labelledby="{{$sub->id}}-tab" >
<table class="table table-bordered">
<tbody>
#foreach($checklist->objectives->where('category_id', $sub->id) as $objective)
<tr>
<th>
{{$objective->competence}} - {{$objective->title}}
</th>
<td>
#switch($objective->pivot->status)
#case('Consistente')
<span class="badge badge-success">{{$objective->pivot->status}}</span>
#break
#case('Difícil de obter')
<span class="badge badge-danger">{{$objective->pivot->status}}</span>
#break
#case('Mais ou Menos')
<span class="badge badge-warning">{{$objective->pivot->status}}</span>
#break
#default
<span class="badge badge-secondary">{{$objective->pivot->status}}</span>
#break
#endswitch
</td>
</tr>
#endforeach
</tbody>
</table>
</div><!-- level content -->
#endforeach
</div><!-- tab-content -->
</div><!-- level content -->
#endforeach
I have tried mostly everything and it does not work.
Hope anyone can help me.. Thanks

Displays categories and sub categories (Laravel Blade)

i have problem with display category and subcategories , i made this
in my blade
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><b class="fa fa-home"></b> Начало</li>
#foreach ($categories as $cat)
#foreach($subcategories as $sub)
#if($sub->parent_id == $cat->id)
<li class="dropdown ">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{$cat->name}} <i class="fa fa-caret-down"></i>
</a>
<ul class="dropdown-menu">
<li>{{ $sub->name }}</li>
</ul>
</li>
#endif
#endforeach
#endforeach
</ul>
</div>
</div>
</nav>
It show categories only when have one subcategory, i don't know why happend! Someone help me! (No bad votes please)
Here is my variables:
$categories = Category::where('parent_id', NULL)->get();
$subcategories = Category::where('parent_id','!=',NULL)->get();
The issue here is that you only create the list item within your foreach loop when there is a sub category aswell.
This example should do the trick for you.
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><b class="fa fa-home"></b> Начало</li>
#foreach ($categories as $cat)
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
{{$cat->name}} <i class="fa fa-caret-down"></i>
</a>
#foreach($subcategories as $sub)
#if($sub->parent_id == $cat->id)
<ul class="dropdown-menu">
<li>{{ $sub->name }}</li>
</ul>
#endif
#endforeach
</li>
#endforeach
</ul>
</div>
It is untested so let me know if it doesn't work.
I fixed it with little changes, but now work thanks #answered26
Here is my changes
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><b class="fa fa-home"></b> Начало</li>
#foreach ($categories as $cat)
<li class="{{ $cat->children->count() > 0 ? 'dropdown' : ''}}">
<a href="#" class="{{ $cat->children->count() > 0 ? 'dropdown-toggle' : '' }}" data-toggle="dropdown">
{{$cat->name}} {!! $cat->children->count() > 0 ? '<b class="fa fa-caret-down"></b>' : '' !!}
</a>
<ul class="dropdown-menu">
#foreach($subcategories as $sub)
#if($sub->parent_id == $cat->id)
<li>{{ $sub->name }}</li>
#endif
#endforeach
</ul>
</li>
#endforeach
</ul>
</div>
</div>

Two ids on the same tbody

I'm trying to display some results on a table based on few dropdown which is used for displaying different results, I choose the parameter followed the date. Unfortunately if I'm not mistaken it is illegal to have two ids on a tbody, as seen my code only takes into consideration of the date and not the parameter selected, so is there any alternative solution to what I'm trying to do?
#extends('app')
#section('header')
<link href="css/reports.css" rel="stylesheet">
#stop
#section('content')
<h1 class="page-header">Reports Archive</h1>
<div class="dropdown">
<a style="font-weight:bold" id="drop5" href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
Zone Options:
<span class="caret"></span>
</a>
<ul id="menu2" class="dropdown-menu" role="menu" aria-labelledby="drop5">
<?php $count = 1; ?>
#foreach ($zones as $zone)
<li role="presentation" class="{{ $count == 0 ? 'active' : '' }}">
<a role="menuitem" tabindex="-1" href="#" data-target="#zone{{ $zone->id }}"> {{ $zone->zone_name }}</a></li>
<?php $count++; ?>
#endforeach
</ul>
</div>
<div role="tabpanel">
<!-- hide the below links-->
<ul class="nav nav-tabs" style="display:none;" role="tablist" id="myTab">
<?php $count = 1; ?>
#foreach ($zones as $zone)
<li role="presentation" class="{{ $count == 0 ? 'active' : '' }}">{{ $zone->zone_name }}</li>
<?php $count++; ?>
#endforeach
</ul>
<!-- Tab panes -->
<div class="tab-content">
<?php $count = 1; ?>
#foreach ($zones as $zone)
<div role="tabpanel" class="tab-pane fade in {{ $count == 0 ? 'active' : '' }}" id="zone{{ $zone->id }}">
<div class="col-md-12">
<h2> {{ $zone->zone_name }} </h2>
<table class="table table-responsive table-hover table-bordered">
<thead>
<tr>
<th style="vertical-align:middle">#</th>
<th>
<div class="dropdown">
<a style="font-weight:bold" id="drop5" href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" role="button" aria-expanded="false">
Parameter Name:
<span class="caret"></span>
</a>
<ul id="menu2" class="dropdown-menu" role="menu" aria-labelledby="drop5">
#foreach ($zone->parameter as $param)
<li role="presentation">
<a class="btn btn-link" data-collapse-group="filters" data-target="#{{ $param->id }}" data-toggle="collapse">{{ $param->parameter_name }}</a></li>
#endforeach
</ul>
</div>
</th>
<th style="vertical-align:middle">Reading Value</th>
<th style="vertical-align:middle">User</th>
<th style="vertical-align:middle">Status</th>
<th style="vertical-align:middle">
<div class="dropdown" style="position:relative">
<a style="font-weight:bold" href="#" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Timestamp
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<?php $validate = "default" ?>
<?php $records = App\Reading::sort()->get() ?>
#foreach ($records as $record)
#if ($record->created_at->year != $validate)
<li>
<a class="trigger right-caret">
{{ $record->created_at->year }}
</a>
<ul class="dropdown-menu sub-menu">
<li>January</li>
<li>February</li>
<li>March</li>
<li>April</li>
<li>May</li>
<li>June</li>
<li>July</li>
<li>August</li>
<li>September</li>
<li>October</li>
<li>November</li>
<li>December</li>
</ul>
</li>
<?php $validate = $record->created_at->year ?>
#endif
#endforeach
</ul>
</div>
</th>
</tr>
</thead>
<?php $validate = "default" ?>
#foreach ($records as $record)
#if ($record->created_at->year != $validate)
<?php $month = 1; ?>
#while ($month != 13)
#foreach ($parameters as $param)
<?php $index = 1; ?>
#if ($param->zone_id == $zone->id)
<tbody id="{{ $record->created_at->year }}-{{ $month }}" class="collapse">
#foreach ($readings as $reading)
#if ($reading->parameter_id == $param->id)
#if ($reading->created_at->year == $record->created_at->year)
#if ($reading->created_at->month == $month)
<tr>
<td>{{ $index }}</td>
<td>{{ $reading->parameter->parameter_name }} </td>
<td>{{ $reading->reading_value }} </td>
<td>{{ $reading->user->name }} </td>
<td>{{ $reading->parameter->threshold->getStatus($reading->reading_value) }} </td>
<td>{{ $reading->created_at }} </td>
</tr>
<?php $index++; ?>
#endif
#endif
#endif
#endforeach
#endif
</tbody>
</div>
#endforeach
<?php $month++; ?>
#endwhile
<?php $validate = $record->created_at->year ?>
#endif
#endforeach
</div>
</table>
</div>
</div>
<?php $count++; ?>
#endforeach
</div>
</div>
#stop
#section('scripts')
<script>
$(function(){
$(".dropdown-menu > li > a.trigger").on("click",function(e){
var current=$(this).next();
var grandparent=$(this).parent().parent();
if($(this).hasClass('left-caret')||$(this).hasClass('right-caret'))
$(this).toggleClass('right-caret left-caret');
grandparent.find('.left-caret').not(this).toggleClass('right-caret left-caret');
grandparent.find(".sub-menu:visible").not(current).hide();
current.toggle();
e.stopPropagation();
});
$(".dropdown-menu > li > a:not(.trigger)").on("click",function(){
var root=$(this).closest('.dropdown');
root.find('.left-caret').toggleClass('right-caret left-caret');
root.find('.sub-menu:visible').hide();
});
});
</script>
#stop
It is illegal to have two ids on the same HTML element, as in this example
<div id="id1" id="id2"> <!-- Here be dragons -->
You can skirt this by just having a delimeter in a single id:
<div id="id1--id2">
Or, you can use arbitrary data attributes:
<div id="" data-date="2015-10-14" data-parameter="zone_id">
Since you're using jQuery, here is a question that specifically asks about how to retrieve a data attribute.

Multi-level navigation using bootstrap in laravel 5

I am using laravel 5 as my framework.
I have a categories table like this:
I want to make it as a bootstrap menu with multi-level if the parent_id = id
This is what I have tried so far:
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
Categories <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
#foreach( $category as $cat )
#if( $cat->id === $cat->parent_id)
<ul class="dropdown-menu" role="menu">
<li class="dropdown-submenu">
<a href="{{ url( '/category', Safeurl::make( $cat->name ) ) }}" data-toggle="dropdown-toggle" aria-expanded="false">{{ $cat->name }}
<ul class="dropdown-menu" role="menu">
<li>
</li>
</ul>
</a>
</li>
</ul>
#else
<li>
{{ $cat->name }}
</li>
#endif
#endforeach
</ul>
</li>
</ul>
But this results is, I get the output as 1 below the other.
I want that if the parent_id = id, it should show the sub category next to the that particular id.
For example, in the current example, id = 6 has the parent_id = 4, that means, the bootstrap menu should show the sub-category link next to the Clothing.
UPDATE 1:
After the answer submitted, I cannot display the sub category.
Here's the code for that:
<li class="dropdown">
Categories <span class="caret"></span>
<ul class="dropdown-menu" role="menu">
#foreach( $category as $cat )
<li #if($cat->childs->count()) class="dropdown" #endif>
<a href="javascript:void(0)" #if( $cat->childs->count() ) class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" #endif>
{{ $cat->name }}
#if( $cat->childs->count() )
<span class="caret"></span>
#endif
</a>
#if( $cat->childs->count() )
<ul class="dropdown-menu" role="menu">
#foreach( $cat->childs as $child )
<a href="{{url('/category', [Safeurl::make($cat->name), Safeurl::make($child->name)])}}">
{{ $child->name }}
</a>
#endforeach
</ul>
#endif
</li>
#endforeach
</ul>
How do I achieve this ?
One way to do this is add a relation in your model
public function childs()
{
return $this->hasMany('Category', 'parent_id', 'id');
}
In your controller select just category where parent_id==0
And in your view you can ask for children:
#foreach( $category as $cat )
<li #if($cat->childs->count()) class="dropdown" #endif>
</span> #endif
#if($cat->childs->count())
<ul class="dropdown-menu" role="menu">
#foreach($cat->childs as $child)
<li></li>
#endforeach
</ul>
#endif
</li>
#endforeach

Categories