Paginate text file in Laravel 8 - laravel

I want to display log file in browser, and that file could be big enough. So, I need to divide it, for example 100 lines per page.
Method looks like that for now:
public function show($request)
{
$file = File::get(storage_path("logs/$request.log"));
return view('backend.log', compact('file'));
}
How to add paginate(100) and how to display it in blade file? It doesn't work without Eloquent, but I don't need database.

Related

Laravel Excel Dynamic Heading Row selection

I'm currently working on a Laravel project using the Laravel-Excel package.
It is working fine, except for a use case i'm trying to solve I'm trying to solve for a few hours now.
Each CSV files I'm reading begins with the Heading Rows, and that's particularly practical, but some of my CSV Files begin with annotations like #CSV DD V2.4.3 at row 1 followed by the Heading Rows at row 2.
So, I need to find out how to determine the real line where the heading rows are located to avoid that unwanted line. I especially need to make it work in the headingRow() method implemented by the WithHeadingRow interface. is there a way t grab lines from the csv file to determine the correct heading row line ?
Hope you'll find out , thanks in advance
I would consider doing this in two steps. This is a very general example, so you will need to add in the appropriate Laravel Excel import concern and other dependencies, perform any checks to make sure the file exists, and so on.
Check the first line of the file before starting the import to determine the correct heading row.
// get the first line of the file
$line = fgets(fopen('path/to/file.csv', 'r'));
// set the heading row based on the line contents
// add terms to search array if desired ['#CSV','...']
$heading_row = Str::startsWith(trim($line), ['#CSV']) ? 2 : 1;
Dynamically inject the heading row value using the WithHeadingRow concern and associated method.
class SampleImport implements WithHeadingRow
{
private $heading_row;
// inject the desired heading row or default to first row
public function __construct($heading_row = 1)
{
$this->heading_row = $heading_row;
}
// set the heading row
public function headingRow(): int
{
return $this->heading_row;
}
}
Now you can pass a custom heading row when running the import.
Excel::import(new SampleImport($heading_row), 'path/to/file.csv');

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');
}

Return laravel query results in chunks

I have an Update model in my Laravel/Vue.js app, instead of retrieving and displaying all results at once in my component, I want to return them in chunks of fives and place a NEXT FIVE UPDATES link in my component to display the next 5 update records, much like in pagination. I tried this:
$update = Update::limit(5)->get();
This does not achieve my desired result. Is there a Laravel method i can use in my laravel backend to send the results in chunks of 5 to my Vue.Js component and then make my NEXT FIVE UPDATES link display the next 5 records.
If you're able to send some kind of page or offset value to the backend then you could use laravel's skip and take methods: https://laravel.com/docs/5.7/queries#ordering-grouping-limit-and-offset
$skip = $request->page; //assuming this variable exists...
$limit = 5;
$update = Update::skip($skip * $limit)->take($limit)->get();

How to handle new incoming entries while paging through posts?

Let's assume I have a pagination like this
return App\Post::paginate(1);
After loading this someone creates a new entry in the database for posts. How can i ensure that the "second page" of my pagination does not return the first one again?
Will I always need to send a timestamp with every request to make an additional query before pagination is used?
like
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);
You can use orderBy. You could do something like that:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);
Or:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();
orderBy means all you result are sorted in your query. Hope it will help.
You don't need to pass any timestamp value to verify pagination.
In the controller, in view file you need to pass result of this query.
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));
In view file you need below line for pagination work automatically as per Laravel syntax.
{{ $result->links() }}
Visit https://laravel.com/docs/5.2/pagination/ how laravel pagination work.

How to echo page number with codeigniter pagination library?

I am using the codeigniter pagination library to page my search results. I am using a limit and an offset to generate the results.
I would like to know how, apart from in the pagination links, echo the number of the page the user is currently viewing.
For example;
You are currently viewing page X out of Y results.
How is this possible??
This also gives the current page no
$currentPage = floor(($this->uri->segment(n)/$config['per_page']) + 1);
where n is the segment
echo "Current Page:".$currentPage;
You could extend the Pagination class (system/libraries/Pagination.php).
Create a file called MY_Pagination.php and put it in application/libraries/
Add this code to your extend pagination class;
class MY_Pagination extends CI_Pagination {
public function __construct() {
parent::__construct();
}
public function current_place() {
return 'You are currently viewing page '.$this->cur_page.' out of '.ceil(($this->total_rows/$this->per_page)). 'results';
}
}
Don't forget to make sure the class extension prefix is set correctly to MY_ in the config.php (about line 109)
Then you can access it in your controller like;
$current = $this->pagination->current_place();
Which you can pass into your view.
Depending on how you are setting up the offset. Ours is setup with an offset of the perpage. So our default is 10 per page. So for page one the offset is 0. For page two the offset is 10 and so on. So for me to get the current page I would take the offset divided by the current limit plus 1. So ($cur_page/$per_page)+1 would get me the current page.

Resources