I copied the table from blade file B which pagination is working fine to blade file A. But the pagination is not working on Blade file A. It does have 10 results shown, but the page number navigation is missing.
Here's a screenshot for reference:
Here is my code:
Blade file A
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<table class="table table-bordered">
<tr>
<th></th>
<th>No</th>
<th>Zone</th>
<th>Code</th>
<th>Description</th>
</tr>
#foreach ($items as $key => $item)
<tr>
<td>{{ Form::checkbox('item[]', $item->id, false, array('class' => 'name')) }}</td>
<td>{{ $loop->index+1 }}</td>
<td>{{ $item->zone }}</td>
<td>{{ $item->code }}</td>
<td>{{ $item->description }}</td>
</tr>
#endforeach
</table>
</div>
</div>
Blade file B:
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Zone</th>
<th>Code</th>
<th>Description</th>
<th width="280px">Action</th>
</tr>
#foreach ($items as $key => $item)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $item->zone }}</td>
<td>{{ $item->code }}</td>
<td>{{ $item->description }}</td>
<td>
{{-- <a class="btn btn-info" href="{{ route('items.show',$item->id) }}">Show</a> --}}
#can('item-edit')
<a class="btn btn-primary" href="{{ route('items.edit',$item->id) }}">Edit</a>
#endcan
#can('item-delete')
{!! Form::open(['method' => 'DELETE','route' => ['items.destroy', $item->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
</table>
Controller A:
public function create(Request $request)
{
$items = Item::paginate(10);
$sites = Site::get(["name", "id"])->all();
// $states = State::all();
// $cities = City::where("state_id",14)->get(["name", "id"]);
return view('services.create', compact('sites', 'items'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
Controller B:
public function index(Request $request)
{
$items = Item::orderBy('id', 'DESC')->paginate(10);
return view('items.index', compact('items'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
This is the problem with the new Version laravel.
In your App\Providers\AppServiceProvider class, you need to add the code below inside the boot() function to support the bootstrap paginator.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
At the end of Blade </table> please add this.
<div class="d-felx justify-content-center">
{{ $items->links() }}
</div>
Related
I wanted to add pagination to my project and followed a tutorial for that. When I click new page the results are not changing.
In my Controller I added this $competitions = Competition::latest()->paginate(1);
Then into app/Providers/AppServiceProvider added this use Illuminate\Pagination\Paginator; and inside the boot function added this: Paginator::useBootstrap();
And the last thing was to add this {{ $competitions->onEachSide(1)->links() }} after I close my table in the blade file.
EDIT:
Controller function:
public function index()
{
$competitions = Competition::latest()->paginate(1);
return view('competition.index', compact('competitions'));
}
Loop in blade:
#foreach ($competitions as $competition)
<tbody >
#switch($competition->status)
#case('active')
<tr>
#break
#case('to_push')
<tr class="bg-secondary">
#break
#case('inactive')
<tr class="bg-warning">
#break
#default
<tr>
#endswitch
<td>{{ $competition->id }}</td>
<td>{{ $competition->name }}</td>
<td>{{ $competition->status }}</td>
<td>{{ $competition->mode }}</td>
<td>{{ $competition->type }}</td>
<td>{{ $competition->frequency }}</td>
<td>{{ $competition->last_push }}</td>
<td>{{ $competition->next_push }}</td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
<td class="text-center">
<form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">
<a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $competitions->links('pagination::bootstrap-5') }}
dd(Competition::all()) returns the 4 records I have
Can you please tell me what I did wrong? Any and all help gratefully received.
$competitions = Competition::latest()->paginate(1) this command will return you one result and then pagination will take 1 per page so it won't be shown at all.
Try something like this in your controller
$competitions = Competition::paginate(1)
return view(...., ['competitions' => $competitions]);
and in your blade add this under the foreach loop where you display data
{{ $competitions->links() }}
Read more here
HERE IS HOW TO MAKE PAGINATION
BLADE
#foreach($paginate as $p)
{{ $p->name }} <br>
#endforeach
{{ $paginate->links() }}
CONTROLLER
return view('backend.test', [
'paginate' => Product::paginate(5)
]);
I added pagination in my application. The same results are displayed on each page and according to the URL, the pages are definitely changing. I've tried a ton of solutions but none worked. Please help me find what I did wrong
My Controller:
public function index(Request $request)
{
$competitions = Competition::latest()->paginate(1);
return view('competition.index', compact('competitions'));
}
Inside app/Providers/AppServiceProvider I added use Illuminate\Pagination\Paginator; and my function looks like this:
public function boot()
{
Paginator::useBootstrap();
}
That's how I loop in view:
#foreach ($competitions as $competition)
<tbody >
#switch($competition->status)
#case('active')
<tr>
#break
#case('to_push')
<tr class="bg-secondary">
#break
#case('inactive')
<tr class="bg-warning">
#break
#default
<tr>
#endswitch
<td>{{ $competition->id }}</td>
<td>{{ $competition->name }}</td>
<td>{{ $competition->status }}</td>
<td>{{ $competition->mode }}</td>
<td>{{ $competition->type }}</td>
<td>{{ $competition->frequency }}</td>
<td>{{ $competition->last_push }}</td>
<td>{{ $competition->next_push }}</td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
<td class="text-center">
<form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">
<a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $competitions->links() }}
Is it right approach to write Laravel PHP code to give a role to hr to view certain column? Because after hosting the website stops automatically and start automatically. Code is working fine..
I am new to PHP, I have no idea if i am going wrong in this code can anyone help me out yrr...
<table class="table table-striped mb-0 dataTable">
<thead>
<tr>
#role('company')
<th>{{__('Employee Name')}}</th>
#endrole
#role('hr')
<th>{{__('Employee Name')}}</th>
#endrole
<th>{{__('Designation')}}</th>
<th>{{__('Promotion Title')}}</th>
<th>{{__('Promotion Date')}}</th>
<th>{{__('Description')}}</th>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<th width="200px">{{__('Action')}}</th>
#endif
</tr>
</thead>
<tbody class="font-style">
#foreach ($promotions as $promotion)
<tr>
#role('company')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
#role('hr')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
<td>{{ !empty($promotion->designation())?$promotion->designation()->name:'' }}</td>
<td>{{ $promotion->promotion_title }}</td>
<td>{{ \Auth::user()->dateFormat($promotion->promotion_date) }}</td>
<td>{{ $promotion->description }}</td>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<td>
#can('Edit Promotion')
<i class="fas fa-pencil-alt"></i>
#endcan
#can('Delete Promotion')
<i class="fas fa-trash"></i>
{!! Form::open(['method' => 'DELETE', 'route' => ['promotion.destroy', $promotion->id],'id'=>'delete-form-'.$promotion->id]) !!}
{!! Form::close() !!}
#endif
</td>
#endif
</tr>
#endforeach
</tbody>
</table>
You can find what you are asking in the documentation here (version select on the left):
https://spatie.be/docs/laravel-permission/v5/basic-usage/blade-directives
#role('hr')
I am from hr!
#else
I am not from hr...
#endrole
It further reads #hasrole('hr') is an alias of #role('hr'), so no worries there.
Similarly, the same goes for permissions:
#can('edit articles')
//
#endcan
For more information, see link mentioned above.
This question already has answers here:
How to stop a loop PHP
(4 answers)
Short circuit Array.forEach like calling break
(30 answers)
How to break a foreach loop in laravel blade view?
(5 answers)
Closed 3 years ago.
I'm setting up a loop whenever i tried to print table first row print good with category name and then next row print table with ONE tr(row) data and closed the tag and again start new row and print next data with one row...
i want to print all data with respect to its category name without break multiple table
#if( ! empty($packages) )
#php
$serviceId=null;
#endphp
#foreach($packages as $package)
#if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $package->service->name }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId=$package->service->id
#endphp
#endif
#if($serviceId != $package->service_id)
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $package->service->name }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId = $package->service_id
#endphp
#endif
<!--SERVICE NAME TABLE-->
<div class="row">
<div class="col-sm-12">
<div class="well">
<div class="table-responsive">
<table class="table table-striped table-sm ">
<thead>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ $package->id }}</td>
<td>{{ $package->name }}</td>
<td>#php $price = isset($userPackagePrices[$package->id]) ? $userPackagePrices[$package->id] : $package->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
<td>{{ $package->minimum_quantity }}</td>
<td>{{ $package->maximum_quantity }}</td>
<td>{{ $package->description }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
!--SERVICE NAME TABLE-->
#endforeach
#else
No Record Found
#endif
You can create an extra array to map this,
In method
$packagesNew = [];
foreach($packages as $k => $v)
{
$packagesNew[$v->service->name]['name'] = $v->service->name;
$packagesNew[$v->service->name]['id'] = $v->service->id;
$packagesNew[$v->service->name]['service_id'] = $v->service_id;
$packagesNew[$v->service->name]['data'][] = $v;
}
pass packagesNew to view
Then I made changes in view, please check.
#if( ! empty($packagesNew) )
#php
$serviceId=null;
#endphp
#foreach($packagesNew as $k => $package)
#if(is_null($serviceId))
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $k }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId=$package['id'];
#endphp
#endif
#if($serviceId != $package['service_id'])
<!--CATEGORY NAME PRINT-->
<div class="row" id="">
<div class="col-sm-12">
<div class="well wellHeader">
<h2>{{ $k }}</h2>
</div>
</div>
</div>
<!--CATEGORY NAME PRINT-->
#php
$serviceId = $package['service_id'];
#endphp
#endif
<!--SERVICE NAME TABLE-->
<div class="row">
<div class="col-sm-12">
<div class="well">
<div class="table-responsive">
<table class="table table-striped table-sm ">
<thead>
<tr>
<th>#lang('general.package_id')</th>
<th>#lang('general.name')</th>
<th>#lang('general.price_per_item') {{ getOption('display_price_per') }}</th>
<th>#lang('general.minimum_quantity')</th>
<th>#lang('general.maximum_quantity')</th>
<th>#lang('general.description')</th>
</tr>
</thead>
<tbody>
#foreach($package['data'] as $k1 => $v1)
<tr>
<td>{{ $v1->id }}</td>
<td>{{ $v1->name }}</td>
<td>#php $price = isset($userPackagePrices[$v1->id]) ? $userPackagePrices[$v1->id] : $v1->price_per_item;
#endphp
{{ getOption('currency_symbol') . number_formats(($price * getOption('display_price_per')),2, getOption('currency_separator'), '') }}</td>
<td>{{ $v1->minimum_quantity }}</td>
<td>{{ $v1->maximum_quantity }}</td>
<td>{{ $v1->description }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
!--SERVICE NAME TABLE-->
#endforeach
#else
No Record Found
#endif
I know it sounds stupid but I've got strange issue with deleting my subcategories, when I try to delete my sub it gives me error that name and slug is required! seems I try to add input in database!
here is some images to make it clear for you:
Here is my controller destroy function:
public function destroy($id)
{
$subcategory = Subcategory::find($id);
$subcategory->delete();
Session::flash('success', 'Your Sub-Category Deleted successfully!');
return redirect()->route('subcategories.index');
}
And this is my form which you see in images above:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Subcategory Name</th>
<th>Slug</th>
<th>Parent Category</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
#foreach ($categories as $category)
#foreach($category->subcategories as $sub)
<tr>
<td>{{ $sub->id }}</td>
<td>{{ $sub->name }}</td>
<td>{{ $sub->slug }}</td>
<td>{{ $sub->category->name }}</td>
<td>
Edit
{!! Form::model(['route' => ['subcategories.destroy', $sub->id], 'method' => "DELETE"]) !!}
{{ Form::submit('Delete', ['class' => 'btn btn-sm btn-danger']) }}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
I think it caused the Form::open return:
<form method="DELETE" action="{{route('subcategories.destroy', ['id' => $sub->id])}}">
I think that is the problem, I have same probleme here and I have read about this in Laracast.
So in my case I change not use Form::open(). I use manual form, and my code looked like this:
<form method="POST" action="{{route('subcategories.destroy', ['id' => $sub->id])}}">
{{ method_field('DELETE') }}
.
.
.
</form>
I hope it can help you :)