Dompdf LoadView variable passing - laravel-5

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

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 Display the results in a table format [duplicate]

This question already has answers here:
Horizontal table layout using CSS
(4 answers)
Closed 2 years ago.
I want to display the result of the permissions table in a table format... I'm using Laratrust for roles and permissions
my RoleController
public function show($id)
{
$role = Role::with('permissions')->with('users')->where('id', $id)->first();
return view('admin.roles.show', compact('role'));
}
I want the results to be display in this table horizontally like the way I hard-coded it
show.blade.php
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">PERMISSION</th>
<th scope="col">CREATE</th>
<th scope="col">READ</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="col">Users</th>
<td>Create User</td>
<td>Read User</td>
<td>Update User</td>
<td>Delete User</td>
</tr>
<tr>
<th scope="col">Profile</th>
<td>Create Profile</td>
<td>Read Profile</td>
<td>Update Profile</td>
<td>Delete Profile</td>
</tr>
</tbody>
</table>
<thead class="thead-dark">
<tr>
<th scope="col">PERMISSION</th>
<th scope="col">CREATE</th>
<th scope="col">READ</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
#foreach($role as $key => $data)
<tr>
<th>{{$data->permission}}</th>
<th>{{$data->create}}</th>
<th>{{$data->read}}</th>
<th>{{$data->update}}</th>
<th>{{$data->delete}}</th>
</tr>
#endforeach
</tbody>
I think this should work if role variable have all the data
You can read in Laravel docs for more information https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
And make sure in your Role model has permissions, users relationship then try this:
// controller file
public function show($id)
{
$role = Role::with(['permissions', 'users'])->where('id', $id)->first();
return view('admin.roles.show', compact('role'));
}
// blade file
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">PERMISSION</th>
<th scope="col">CREATE</th>
<th scope="col">READ</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
#foreach($role->permissions as $permission)
<tr>
<th scope="col">{{ $permission->name }}</th>
<td>Create User</td>
<td>Read User</td>
<td>Update User</td>
<td>Delete User</td>
</tr>
#endforeach
</tbody>
</table>

Laravel 5.7 simple pagination

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

Laravel Excel Export - From View not working

I tried to implement exporting to excel file From View approach using the Laravel Excel. Here is the link of the documentation https://laravel-excel.maatwebsite.nl/3.1/exports/from-view.html. But I can't figure it out yet referencing the example shown in the website. It returns an error saying PhpOffice \ PhpSpreadsheet \ Writer \ Exception
Invalid parameters passed. . I've been changing my codes trying to solve this but no luck at all. Please help me figure this out. Below are my codes. Thank you.
LoansExport.php
<?php
namespace App\Exports;
use App\Loan;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class LoansExport implements FromView
{
public function view(): View
{
return view('partials.view_loan_export', [
'loans' => Loan::all()
]);
}
}
view_loan_export.blade.php
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
</body>
LoansController.php
<?php
namespace App\Http\Controllers;
use App\Loan as Loan;
use App\Member as Member;
use Illuminate\Http\Request;
use App\Exports\LoansExport;
use Maatwebsite\Excel\Facades\Excel;
class LoansController extends Controller
{
public function loanexport()
{
return Excel::download(new LoansExport, 'loans.xlsx');
}
}
web.php
Route::get('/loanexport', 'LoansController#loanexport');
error
I have done By different way
LoansController.php
public function loanexport(){
$loan= array();
$loans= loan::all();
$data = [
'success' => 'success',
'loans' => $loans,
];
return Excel::download(new LoansExport($data), 'loans.xlsx');
}
LoansExport.php
public function __construct($data) {
$this->data = $data;
}
public function view(): View
{
//dd($this->data);
return view('partials.view_loan_export',$this->data);
}
view_loan_export.blade.php
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>
just put the table tag and the tag within it in your view
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
#foreach ($loans as $loan)
<tr>
<td >{{ $loan->member->fname }}</td>
<td >{{ $loan->member->lname }}</td>
</tr>
#endforeach
</tbody>
</table>

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

Resources