I am using mpdf in laravel. But it is not displaying {colsum} in the table footer. using Laravel PDF: mPDF wrapper for Laravel 5.
controller code to pass data to view file.
$data = [
'data' => $vochur,
'date' => date('m/d/Y H:i:s A'),
'setting' => $setting,
'user' => Auth::user()->first_name
];
$pdf = PDFF::loadView('pdf.vouchers', $data);
view file code. using
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td>{colsum}</td>
<td>{colsum}</td>
<td>{colsum}</td>
<td>{colsum}</td>
<td>{colsum}</td>
</tr>
</tfoot>
<tbody>
#foreach ($data['bilties'] as $key => $item)
<tr>
<td>{{$item['id']}}</td>
<td>{{$item['customer']['name']}}</td>
<td>{{$item['product_type']}}</td>
<td>{{$item['quantity']}}</td>
<td>{{$item['fare']}}</td>
<td>{{$item['labour']}}</td>
<td>{{$item['adv']}}</td>
<td>{{$item['total']}}</td>
</tr>
#endforeach
</tbody>
Related
This is how my app generate a report (excel file) from my app:
Export/ActividadesporTodosServiciosExport.php
public function collection()
{
return Actividad::whereMonth('fecha_inicio', '12')
->whereYear('fecha_inicio', '2022')
->orderBy('servicio_id', 'ASC')
->with('servicio')
->get();
}
public function headings(): array
{
return [
'SERVICIO',
'DESCRIPCION',
];
}
public function map($actividad) : array
{
$nombre = [];
$descripcion = [];
foreach($actividad as $activity){
// dump($actividad);
$nombre[]=$actividad->descripcion;
foreach($actividad->servicio as $key => $servicio){
$descripcion = $actividad->servicio->nombre;
}
}
return [
[
$nombre[0],
$descripcion,
'',
'',
],
];
}
The screenshot shows 4 records in 4 rows, and I wanna try to convert that 4 records in one only cell of a row, like this example:
You could do that easily in a view.
https://docs.laravel-excel.com/3.1/exports/from-view.html
namespace App\Exports;
use App\Models\Actividad;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class ActividadesporTodosServiciosExport implements FromView
{
public function view(): View
{
$actividades = Actividad::query()
->whereMonth('fecha_inicio', '12')
->whereYear('fecha_inicio', '2022')
->orderBy('servicio_id', 'ASC')
->with('servicio')
->get();
return view('exports.your_view', [
'actividades' => $actividades,
]);
}
}
resources/views/exports/your_view.blade.php
<table>
<thead>
<tr>
<th>SERVICIO</th>
<th>DESCRIPCION</th>
</tr>
</thead>
<tbody>
#foreach($actividades as $actividad)
<tr>
<td>{{ $actividad->descripcion }}</td>
<td>{{ $actividad->servicio->implode('nombre', "\n") }}</td>
</tr>
#endforeach
</tbody>
</table>
Alternatively using the rowspan attribute to get grouped cells in the spreadsheet
<table>
<thead>
<tr>
<th>SERVICIO</th>
<th>DESCRIPCION</th>
</tr>
</thead>
<tbody>
#foreach($actividades as $actividad)
<tr>
<td rowspan="{{ $actividad->servicio->count() + 1">{{ $actividad->descripcion }}</td>
</tr>
#foreach($actividad->servicio as $servicio)
<tr>
<td>{{ $servicio->nombre }}</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
Problem: There are modules, users, and user_modules tables, where the admin can assign multiple modules with permissions to a user. Admin can update module's permission which is already assigned to that user, and the modules which are not assigned should be loaded on blade view on the same table.
But the issue is data is duplicating
I am posting my code with images
AdminController:
$modules = Module::all();
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules'));
seller_assign_modules.blade.php
<table class="table table-striped">
<thead>
<tr>
<th>Modules</th>
<th>Add</th>
<th>Edit</th>
<th>View</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
#foreach ($user_modules as $user_mod)
#foreach ($modules as $mod)
#if ($mod->id == $user_mod->module_id)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#else
<tr>
<td scope="row">{{$mod->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endif
#endforeach
#endforeach
</tbody>
</table>
modules table:
user_modules table:
result on seller_assign_modules.blade.php
I NEED THIS:
You can do something like below (this is not a good approach however)
$user_modules = User_module::with('module')->where('user_id', $user_id)->get();
//Get modules where user dont have records
$non_user_modules = Module::whereNotIn('id', (clone $user_modules)->pluck('module_id')->toArray())->get();
return view('admin/seller_assign_modules', compact('user','modules','user_modules','non_user_modules'));
Then in the blade
#foreach ($user_modules as $user_mod)
<tr>
<td scope="row">{{$user_mod->module->name}}</td>
<td scope="row">{{$user_mod->add}}</td>
<td scope="row">{{$user_mod->edit}}</td>
<td scope="row">{{$user_mod->view}}</td>
<td scope="row">{{$user_mod->del}}</td>
</tr>
#endforeach
#foreach ($non_user_modules as $non_user_module)
<tr>
<td scope="row">{{$non_user_module->name}}</td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
<td scope="row"></td>
</tr>
#endforeach
Best approach you could do
Make a relationship at Module model like userModules()
Then get Modules like below
$modulesWithUser = Module::with(['userModules' => function($userQuery){
$userQuery->where('user_id',$user_id);
}])->get();
Then loop $modulesWithUser in blade and access its relationship inside loop to show users permissions.
The problem with here is,Module has manyUser_modules, but you only need one.
So in this case, one approach is map a new property like user_module to each Module.
AdminController
$modules = Module::all();
$userModeules = User_module::where('user_id', $user->id)->get();
$modules = $modules->map(function($module) {
$module->user_module = $userModules->firstWhere('module_id', $module->id);
return $module;
});
return view(
'admin/seller_assign_modules',
compact('modules')
);
seller_assign_modules.blade.php
<tbody>
#foreach($mdules as $module)
<tr>
<td scope="row">{{ $module->name }}</td>
<td scope="row">{{ optional($module->user_module)->add }}</td>
<td scope="row">{{ optional($module->user_module)->edit }}</td>
<td scope="row">{{ optional($module->user_module)->view }}</td>
<td scope="row">{{ optional($module->user_module)->del }}</td>
</tr>
#endforeach
</tbody>
Using Laravel 8
My route:
Route::get('socios/export', [SocioController::class, 'export'])->name('socios.export');
My Controller method:
public function export(Request $request)
{
$registros = User::socios()->get();
$content = view('socios.excel', compact('registros'));
$status = 200;
$headers = [
'Content-Type' => 'application/vnd.ms-excel; charset=utf-8',
'Content-Disposition' => 'attachment; filename="filename.xls"',
];
$response = response($content, $status, $headers);
return $response;
}
My 'socios.excel' view:
<table>
<thead>
<tr>
<th>ID</th>
<th>name</th>
<th>...</th>
</tr>
</thead>
<tbody>
#foreach ($registros as $registro)
<tr>
<td>{{ $registro->id }}</td>
<td>{{ $registro->name }}</td>
<td>...</td>
</tr>
#endforeach
</tbody>
</table>
The file exports fine but string like "Dirección" contains wrong chars:
I had try different charset in content-type header but can not fix it.
Some help?
Thanks!
I have a large single pdf document which consists of multiple records.I need to break or split the table records into second pages in the pdf using laravel.
Using the below codings in the controller the table records are not fully displayed.Some table records are missing in that pdf file.so I need to show the missing table records into second pafe of the pdf file.
private function generatePdf($customerstatement, $content) {
if (!is_dir(RECEIPTS_DIR)){
mkdir(RECEIPTS_DIR, 0755, true);
}
$outputName = $customerstatement->card_id . '-' . strtoupper(date("MY")) .'.pdf';
$pdfFileName = RECEIPTS_DIR.'/' . $outputName;
$view = View::make("admin.document.statement", compact('customerstatement', 'content'))->render();
$pdf = new \Thujohn\Pdf\Pdf();
$bytes_written = File::put($pdfFileName, $pdf->load($view, 'A4', 'portrait')->output());
if ($bytes_written === false) {
throw new \Exception("Unable to produce pdf statement");
}
$customerstatement->pdffile = $outputName;
$customerstatement->save();
return $outputName;
}
The below codings are written in the blade file for showing the records in the pdf.
<tbody>
<tr>
<td colspan="7"><b>{{ $customerstatement->customer->card_id }} - {{ $customerstatement->customer->fullName }} </b>
</td>
</tr>
<tr>
<td colspan="7"><b>Beginning Balance : {{ $customerstatement->beginning_balance }}</b>
</td>
</tr>
#foreach ($customerstatement->statements as $statement)
<tr>
<td width="7em">{{{ $statement->date }}}</td>
<td width="15em">{{{ $statement->memo }}}</td>
<td width="6em" align="center">{{{ $statement->debit }}}</td>
<td width="6em" align="center">{{{ $statement->credit}}}</td>
<td width="6em" align="center">{{{ $statement->closing_balance}}}</td>
</tr>
#endforeach
</tbody>
<tr>
<td colspan="7"><b>
{{ $customerstatement->customer->card_id }} - {{ $customerstatement->customer->fullName }} </b></td>
</tr>
<tr><td colspan="7"><b>Beginning Balance : {{ $customerstatement->beginning_balance }}</b></td></tr>
<?php $i=0 ?>
#foreach ($customerstatement->statements as $statement)
<?php $i++ ?>
<tr>
<td width="7em">{{{ date("d-m-Y", strtotime($statement->date) )}}}</td>
<td width="15em">{{{ $statement->memo }}}</td>
<td width="6em" align="center">{{{ $statement->debit }}}</td>
<td width="6em" align="center">{{{ $statement->credit}}}</td>
<td width="6em" align="center">{{{ $statement->closing_balance}}}</td>
<td ><?php
if($i==25){ ?>
<?php $i=0 ?>
<div style="page-break-after: always;"></div>
<?php }
?></td>
</tr>
I am trying to output a multidimensional array in my view, but am getting some wacky output on the table. Here is a screen grab of what I have:
Here is the code I am using:
</tr>
#endforeach
</tbody>
</table>
I think it has something to do with my if else statement, but have been stuck on this problem for some time now. Any help would be much appreciated! Thanks
EDIT: As requested, here is my data structure and it's var_dump:
It is hard to tell without your data structure, but I imagine the issue is with your data structure and your nested foreach.
When you are checking the $status you loop through the array. The first time you loop through the array $status == 1, but a td is being written every time you check the status.
#foreach ($count as $status => $c)
#if($status == 2)
<td>{{$c}}</td>
#else
<td> </td>
#endif
...
#endforeach
So on the first pass assuming $status = 1; You will get
<td> </td>
<td>{{$c}}</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
on the second pass, assuming $status = 2 you will get:
<td>{{$c}}</td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
but you are still on the first person.
What you might consider is changing the structure of your data to something like this. This is psuedo code so please modify as necessary.
$person = [
$totals => [
$qualifiedProspectCount => 2,
$unqualifiedProspectCount => 2,
$contactedCount => 2,
$qualifiedProspectCount => 2,
$convertedToAccountCount => 2,
$pendingIntegrationCount => 2,
$revenueGeneratingCountCount => 2
]
];
Then only gling through your data once like this. I extracted the into a partial, but you could continue to do it inline.
page.blade.php
<table class="table tablesorter" id="tblAffs">
<thead>
<tr class="tblhead">
<th>AM</th>
<th>Qualified Prospect</th>
<th>Unqualified Prospect</th>
<th>Contacted</th>
<th>Converted To Account</th>
<th>Pending Integration</th>
<th>Revenue Generating</th>
</tr>
</thead>
<tbody>
$totalCount should be a collection of $person
#foreach ($totalCount as $name => $totals)
<tr>
<td>
{{$name}}
<br>
Closed
</td>
#include('partial.item', 'count' => $totals->qualifiedProspectCount)
#include('partial.item', 'count' => $totals->unqualifiedProspectCount)
#include('partial.item', 'count' => $totals->contactedCount)
#include('partial.item', 'count' => $totals->convertedToAccountCount)
#include('partial.item', 'count' => $totals->pendingIntegrationCount)
#include('partial.item', 'count' => $totals->revenueGeneratingCountCount)
</tr>
#endforeach
</tbody>
partial/item.blade.php
#if($count > 0)
<td>{{{$count}}}</td>
#else
<td> </td>
#endif
EDIT
So this is going to get you into all kinds of trouble later on, but here you go.
#foreach ($totalCount as $name => $count)
<tr>
<td>
{{$name}}
<br>
Closed
</td>
#if(isset($count[2]))
<td>{{ $count[2] }}</td>
#else
<td> </td>
#endif
#if(isset($count[1]))
<td>{{ $count[1] }}</td>
#else
<td> </td>
#endif
// and so on ...
</tr>
#endforeach
I'd recommend checking out Laravel's Documentation, there are many ways to do what you are trying to do that are far easier than how you are going about it. Even though it feels faster. It isn't.