Laravel 5.7 simple pagination - laravel

In our application, we have modules called Reports where in this report will show you the summarize of the module.
Example
General Journal Report - in this report will show you the summarize of the general journal module.
Now in our reports need to implement the pagination, having a problem applying the pagination in query builder.
Controller
$general_journals = GeneralJournalReportModel::getGeneralJournals();
$data['defined_gj'] = GeneralJournalReportItemsController::getDefinedGJById($general_journals);
I fetch all the general journal records and stored it in the variable general_journals then I passed it in another controller to do something else(such as putting some error handling when the column is null).
Model
public static function getGeneralJournals()
{
return DB::table('general_journals as gj')
->select('gj.id as id',
'gj.number as number',
'gj.posting_date as posting_date',
'gj.remarks as remarks',
'gj.document_reference as document_reference',
'gji.debit_amount as debit_amount',
'gji.credit_amount as credit_amount')
->leftJoin('general_journal_items as gji', 'gj.id', '=', 'gji.general_journal_id')
->where('gj.company_id', Auth::user()->company_id)
->where('gj.approval_status', 3)
->orderBy('gj.id', 'desc')
->simplePaginate(5);
}
View
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12 col-lg-12">
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover general-journal-report-table" id="gj-report">
<thead class="thead-global">
<tr>
<th id="pj_sequence">Sequence No.</th>
<th id="pj_date">Posting Date</th>
<th id="pj_op">Transaction No.</th>
<th id="4">Document Reference</th>
<th id="5">Remarks</th>
<th id="6">Amount Due</th>
</tr>
</thead>
<tbody class="general-journal-report-details">
#if($defined_gj)
<?php $counter = 0; ;?>
<?php $total_debit = 0; ?>
#foreach($defined_gj as $key => $value)
<?php $counter++;?>
<?php $total_debit += $value->debit ;?>
<tr>
<td class="pj_sequence">{{$counter}}</td>
<td class="pj_date">{{$value->posting_date}}</td>
<td class="pj_op">{!! $value->number !!}</td>
<td>{{$value->doc_ref}}</td>
<td>{{$value->remarks}}</td>
#if($value->debit == '0')
<td></td>
#else
<td align="right"> {{number_format($value->debit,2)}}</td>
#endif
</tr>
#endforeach
<tr>
<td><b>Total</b></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td align="right"> {{number_format($total_debit)}}</td>
</tr>
#endif
</tbody>
</table>
</div>
{{ $general_journals->links() }}
</div>
When I used the links() my button doesn't have any class.
Question: How do I put some class on the button of links?
NOTE: I tried this {{ $general_journals->links() }} but it only worked on the previous button(I want to also apply the class in the next button)

Try this
{{ $general_journals->links('pagination::bootstrap-4') }}
Replace with your laravel bootstrap version.

If you want to customize the pagination view, you can follow to this link https://laravel.com/docs/5.7/pagination#customizing-the-pagination-view

Related

Laravel Livewire checkbox checked on edit page

I'm facing an issue with checking the checkbox on the edit page using Livewire. I've tried many ways but still not able to check the old selected value
View Code:
<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds.{{$property->id}}"
value="{{$property->id}}" #if(in_array($property->id,$propertyIds)) checked #endif>
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>
Backend Code:
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}```
updated
And can you check if this correct? wire:model="propertyIds.{{$property->id}}"? Try to delete the wire:model .
<input class="branchCheckbox" type="checkbox"
value="{{$property->id}}"
#if(in_array($property->id,$propertyIds)) checked #endif>
`<table id="branches" class="table table-bordered table-sm" width="100%" cellspacing="0">
<thead>
<tr>
<th width="10%">
</th>
<th>Branch ID</th>
</tr>
</thead>
<tbody>
#foreach ($propertiesOptions as $key => $property)
<tr>
<td>
<input class="branchCheckbox"
type="checkbox"
wire:model="propertyIds"
value="{{$property->id}}" >
</td>
<td>{{$property->id}}</td>
</tr>
#endforeach
</tbody>
</table>`
BACKEND
public $propertyIds, $propertiesOptions;
public function mount($record)
{
$this->propertiesOptions = Properties::where('merchant_id',$record->id)->get()
$this->propertyIds = $record->properties->pluck('property_id')->toArray();
}

How to create a template view table tag without repeating table tags on every file

I'm developing a system which has many tables so I have to repeat the writing of table tags on all the files which display a table
Here is what I'm doing on every file which have to display table
On Countries table:
<table class="table table-striped table-hover table-sm" id="table">
<thead class="text-uppercase text-center bg-primary">
<tr class="text-white">
<th scope="col">Name</th>
<th scope="col">Slug</th>
<th scope="col">Population</th>
<th scope="col">Region</th>
<th scope="col">Cities</th>
<th scope="col">Descriptions</th>
<th scope="col" >Actions</th>
</tr>
{{ csrf_field() }}
</thead>
<tbody>
#foreach ($countries as $country)
<tr>
<td class="text-left">{{$country->name}}</td>
<td class="text-center">{{$country->slug}}</td>
<td class="text-right">{{$country->population}}</td>
<td class="text-center">{{$country->region->name}}</td>
<td class="text-center">{{$country->city_count}}</td>
<td class="text-left">{{$country->desc}}</td>
<td class="text-center">
<div class="btn-group">
#can('country-update')
<a class="btn btn-primary btn-sm mr-2" href="{{route('location.countries.edit',$country)}}" title="Edit"><i class="fa fa-edit"></i></a>
#endcan
#can('country-delete')
<form class="form-delete" method="post" action="{{route('location.countries.destroy',$country)}}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')"><i class="fa fa-trash-alt"></i></button>
</form>
#endcan
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
then on the cities I'll do the same but with different table head names and table data
<table class="table table-striped table-hover table-sm" id="table">
<thead class="text-uppercase text-center bg-primary">
<tr class="text-white">
<th scope="col">Name</th>
<th scope="col">Slug</th>
<th scope="col">Country</th>
<th scope="col">Descriptions</th>
<th scope="col" >Actions</th>
</tr>
{{ csrf_field() }}
</thead>
<tbody>
#foreach ($cities as $city)
<tr>
<td class="text-left">{{$city->name}}</td>
<td>{{$city->slug}}</td>
<td class="text-center">{{$city->country->name}}</td>
<td class="text-left">{{$city->desc}}</td>
<td class="text-center">
<div class="btn-group">
#can('city-update')
<a class="btn btn-primary btn-sm mr-3" href="{{route('location.cities.edit',$city)}}" title="Edit"><i class="fa fa-edit"></i></a>
#endcan
#can('city-delete')
<form class="form-delete" method="post" action="{{route('location.cities.destroy',$city)}}">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?')" title="delete"><i class="fa fa-trash-alt"></i></button>
</form>
#endcan
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
but I wat to have a template where I'll only populate table head rows and table body something like this
<div class="table-responsive">
<table class="table table-striped table-hover table-sm" id="table">
<thead class="text-uppercase text-center">
<tr>
//Dynamic table heads
#if(!is_null($rows))
#foreach($rows as $row)
{{$row}}
#endforeach
#endif
</tr>
</thead>
<body>
//Dynamic table body
#if(!is_null($datas))
#foreach($datas as $data)
{{$data}}
#endforeach
#endif
</tbody>
</table>
<!-- end table -->
</div>
What is the best way to accomplish this
You can use Blade component for that. One of approaches in Laravel 7 is to use Blade class component. Link to official Blade components docs: https://laravel.com/docs/7.x/blade#components
You can create a generic table component using artisan command:
php artisan make:component Table
Component class
Your component could look like that:
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class Table extends Component
{
/**
* Array of table heading strings
*
* #var array
*/
public $headings;
/**
* Table rows
*
* #var array
*/
public $rows;
/**
* Create the component instance.
*
* #param string $type
* #param string $message
* #return void
*/
public function __construct($headings, $rows)
{
$this->headings = $headings;
$this->rows = $rows;
}
/**
* Get the view / contents that represent the component.
*
* #return \Illuminate\View\View|string
*/
public function render()
{
return view('components.table');
}
}
Component view
Now when you have component class, you have to prepare the component view. You will found it in /resources/views/components/table.blade.php.
<div class="table-responsive">
<table class="table table-striped table-hover table-sm" id="table">
//Dynamic table heads
#if($headings)
<thead class="text-uppercase text-center">
<tr>
#if($rows))
#foreach($rows as $row)
<th>{{$row}}</th>
#endforeach
#endif
</tr>
</thead>
#endif
<tbody>
//Dynamic table body
#foreach($rows as $row)
<tr>
// Render each item from row
#foreach($row as $item)
<td>{{$item}}</td>
#endforeach
</tr>
#endif
</tbody>
</table>
<!-- end table -->
</div>
Now you can use your component in your views:
<x-table :headings="['foo', 'bar']"
:data="[['foo1', 'bar1'], ['foo2', 'bar2']]" class="mt-4"/>

Foreach loop has undefined variable in my view

The error is on the view which says that
my $audio variable is not defined.
I want to show everything from the database using a foreach loop.
I tried base on paginate on the laravel documentation https://laravel.com/docs/5.7/pagination#basic-usage
My controller is below:
use Illuminate\Http\Request;
use App\Audio_lectures;
use Illuminate\Support\Facades\DB;
public function index(){
$audio = DB::table('audio_lectures')->latest()->paginate();
return view('welcome', ['audio_lectures' => $audio]);
}
The welcome view is below:
<div class="container">
<div class="row">
<table class="table table-dark">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Subject</th>
<th scope="col">Date Added</th>
<th scope="col">Download</th>
</tr>
</thead>
<tbody>
#foreach ($audio as $audio_lectures)
<tr>
<th scope="row">{{$audio_lectures->id}}</th>
<td>{{$audio_lectures->subject}}</td>
<td>{{$audio_lectures->created_at}}</td>
<td>Download</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
My route web.php is below:
Route::get('/','Audio_lecturesController#index');
I expect to show all the audio lectures from my database to my view.
try this one
#foreach ($audio_lectures as $audio)
<tr>
<td scope="row">{{$audio->id}}</td>
<td>{{$audio->subject}}</td>
<td>{{$audio->created_at}}</td>
<td>
Download
</td>
</tr>
#endforeach
you are passing from controller audio_lectures array but you
getting as in your view audio
On your view file looping try this
#foreach ($audio_lectures as $value)
<tr>
<td scope="row">{{$value->id}}</td>
<td>{{$value->subject}}</td>
<td>{{$value->created_at}}</td>
<td>Download</td>
</tr>
#endforeach

Dompdf LoadView variable passing

i can't load able to pass a parameter to the dompdf function LoadView.
this is my controller:
public function pst_affluiti(){
$data=DB::all('Anagrafica')->get();
$pdf = PDF::loadView('output',['data' => $data]);
return $pdf->stream('output.pdf');
}
my output.blade.php
<body>
<div class="container">
<table class="table table-sm table-bordered" >
<thead>
<tr>
<th scope="col-sm-1 col-6">Data Affl.</th>
<th scope="col-sm-2">Cron.</th>
<th scope="col-sm-2">Cognome</th>
<th scope="col-sm-2">Nome</th>
<th scope="col-sm-2">Data nac.</th>
</tr>
</thead>
<tbody>
#foreach($data as $row)
<tr>
<td>{{$row->Cron}}</td>
<td>{{$row->Surname}}</td>
<td>{{$row->Name}}</td>
<td>{{$row->Date}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</body>
if i load a normal view of my query it works well.
Any suggest?
Thanks in advance
you can use
compact('data')
instead of
$data

Laravel 5.2 pagination: how to decrease distance from table

I have a Laravel 5.2 application. I'm using the default Laravel pagination. All works functionally but there is quite some empty space between the last row of the table and the pagination link. See picture for an example:
How can I make the empty space a bit less?
Here is the code I'm using:
<table class="table table-striped table-bordered table-list">
<thead>
<tr>
<th class="hidden-xs">Auction id</th>
<th>Name</th>
<th>Description</th>
<th><em class="fa fa-cog"></em>Actions</th>
</tr>
</thead>
<tbody>
#foreach( $events as $event )
<tr>
<td align="center" class="hidden-xs">{{ $auction->id }}</td>
<td>{{ $event>name }}</td>
<td>{{ $event->description }}</td>
<td align="center">
<div>
<em class="fa fa-list-alt"></em>
</div>
</tr>
#endforeach
</tbody>
</table>
<div class="text-right">
{{ $events->render() }}
</div>
<div class="text-right">
<!-- Your pagination code here -->
</div>

Resources