Append Rows to existing Excel document using Laravel-Excel - laravel

I have an existing Excel document, to which I want to append some data using Laravel-Excel. The package has nice documentation but unfortunately there is no full example showing how to do that, but only partial demonstration of manipulating rows.
What I am attempting to do is:
open the existing document
get the first sheet, append new
close the document
The code:
Excel::load($path . '/exported.xls', function($reader){
$sheet = $reader->getActiveSheet();
// Manipulate third row
$sheet->row(3, array(
'test1', 'test2'
));
});
Which results in
Call to undefined method PHPExcel_Worksheet::row()
Has anyone succeeded appending data with this package?

Laravel Excel 1.2.0 added support for appending rows (so modifying existing Excel files)
http://www.maatwebsite.nl/laravel-excel/docs/import#edit
The correct code would be:
Excel::load($path . '/exported.xls', function($reader)
{
$reader->sheet(function($sheet)
{
// Manipulate third row
$sheet->row(3, array(
'test1', 'test2'
));
});
})->export('xls');

It looks like you would use...
$sheet->appendRow(array(
'appended', 'appended'
));
Found in the docs here... http://www.maatwebsite.nl/laravel-excel/docs/export

Right now it is not possible to edit an Excel file like I intended to.
Source: package creator.

Related

Batch printing PDF files with laravel

I have a program that can print an individual PDF when on a students file. I'm doing this using niklasravnsborg/laravel-pdf package in Laravel 5.7. It's working great because I can just stream the pdf from a view into the browser then print from there.
Now I'm wanting to batch print the PDF's at the end of the day instead of one by one. How can I do this? There's no documentation for this on the package repository.
Several ways I've thought of doing this: one, save each PDF as an image file then try to print all files in that folder at the end of the day. If I do this, how would I print all files in that folder?
Next: does anyone know a way to maybe append a new PDF to a variable containing all the previously looped pdf's?
For example:
$finalpdf;
$students = Student::all();
foreach($students as $student){
$pdf = PDF::loadView('pdf.document', $student);
$finalpdf .+ $pdf; //i know this line doesn't work, but how to alter it?
return $pdf->save('document.pdf');
}
After a few days of playing around, hopefully this solution helps someone in the future. I used another package called "lara pdf merger". By writing my files to a save destination, adding those files to a merged file, then deleting the saved files after download, I was able to get my functionality. This is my controller code:
use PDF;
use Illuminate\Filesystem\Filesystem;
use LynX39\LaraPdfMerger\Facades\PdfMerger;
public function batchPrint(){
$pdfMerger = PDFMerger::init();
$i = 0;
$students = Student::whereDate('updated_at', Carbon::today('America/Los_Angeles'))->get();
foreach ($students as $student) {
$pdf = PDF::loadView('printTables', compact('student'));
$pdf->save('pdf/document'.$i.'.pdf');
$pdfMerger->addPDF('pdf/document'.$i.'.pdf');
$i++;
}
$pdfMerger->merge();
$pdfMerger->save("file_name.pdf", "browser");
$file = new Filesystem;
$file->cleanDirectory('pdf');
}

Laravel Path With Different Views

This is my Route
Route::get('sites/{site_id}/report/{report_id}', array('as'=>'Reports'),
function($site_id, $report_id){
// $report = ???
return view('reports')->with('report', $report)->with('site_id',$site_id)-
>with('report_id', $report_id);
});
This is my blade file
<a href="{{route('Reports',['site_id'=>$report->site_id,
'report_id'=>$report- >report_id])}}">view</a>
Here is the problem. I want to make a path for every report. Example is inside this path have a 3 reports http://localhost:8000/sites/1
Then when I click one of the report it should give me this path
http://localhost:8000/sites/1/report/1 (sites/$site_id/report/$report_id)
But it confused me a lot, when you click on report_id = 1 and report_id = 2
The value of the `return view('reports') are still the same how could I make a different views for every reports I have.
You have any thoughts?
You would need to get the information from the database. You can use your App\Report model to get the information for the report:
Route::get('sites/{site_id}/report/{report_id}', ['as' => 'Reports'], function ($site_id, $report_id) {
$report = App\Report::findOrFail($report_id);
return view('reports')->with('report', $report)->with('site_id', $site_id)->with('report_id', $report_id);
});
You can find more information in the documentation

I am new in Laravel can any one tell how can i save number in a table and square of that number into another table using helper?

Actually I am new in Laravel can anyone tell me how I can save a number in a table and the square of that number into another table using helper?
You can use this sample:
$value = 4;
DB::table('table_name')->insert(['column_name' => $value]);
$sqrt_value = sqrt($value);
DB::table('table_name')->insert(['column_name' => $sqrt_value]);
Also you can read documentation here.

Yii2- Not able to insert/delete rows using console controller

I am trying to implement email queue in my project which uses CRON.
I am adding a row in one table and deleting a row from another table using CreateCommand of yii2. Database is mysql.
The entire code is executed and it does not display any error. But the rows are not added or deleted from the database.
I have also tried doing the same thing using ActiveRecords but it does not work. Hence I have used CreateCommand.
Below is the code which I am using -
$connection = \Yii::$app->db;
$result = $connection->createCommand()->insert('email_queue_completed', $arrInsert)->execute();
if(!empty($result))
{
$connection->createCommand()
->delete('email_queue', ['id' => $mail->id])
->execute();
}
$arrInsert is an array containing key value pairs of values to be inserted.

Populating dropdown list with two columns from database

I'm working on MVC3 project.
At my controller I already have a code that gets the Descr column from StockClass table. After that I fill a ViewBag with this list in order to retrieve it from the view and populate the dropdown list.
Currently is working fine but only shows Descr field (obviously). What i want is populate the dropdown list with two fields (Code and Descr) in this format: "code - descr".
I tried several ways but i cannot find the way to code the #Html helper correctly.
In my controller...
var oc = dba.StockClass.OrderBy(q => q.Code).ToList();
ViewBag.OrderClass = new SelectList(oc, "StockClassId", "Descr");
In my view....
#Html.DropDownList("StockClassID", (SelectList)ViewBag.OrderClass)
Could you please help me?
I don't believe there is an HTML Helper which will do that for you, but you can get what you're after like this:
var oc = dba.StockClass
.OrderBy(q => q.Code)
.ToDictionary(q => q.StockClassId, q => q.Code + " - " + q.Descr);
ViewBag.OrderClass = new SelectList(oc, "Key", "Value");
This also has the advantage of making the uses of StockClassId, Code and Descr refactor-friendly - if you rename those properties you won't be able to compile without updating this bit of code to match.

Resources