Skip to content

bernadev91/laravel-reports

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

laravel-reports

Scaffolding to generate data reports in a unified way

To install, simply run the following command:

composer require bernadev/laravel-reports

This package is intended to make the creation of new reports faster and easier to maintain. To do so, it establishes an interface all reports must implement. With this library, when you need to create a new report, all you have to do is the following:

  1. Create a route for it
  2. Write a couple of class methods
  3. Write a Blade template to render the results.

This class helps in the following ways:

  1. Artisan command to create new reports. Just run php artisan reports:generate . It will ask for the details of the new report and it will create a class and a view for it.
  2. Interface all report classes must implement with common methods.
  3. Blade view that shows the list of filters. Using Bootstrap 5.

Filtering

Example of how setting the filters work. You have to create an associative array where the key is the name of the field and it contains a set of properties.

    public function __construct(array $input_data=array())
    {
        $this->filters = [
            'start_date' => ['name' => 'Start Date', 'value' => NULL, 'type' => 'date'],
            'end_date' => ['name' => 'End Date', 'value' => NULL, 'type' => 'date']
        ];

        if (Arr::get($input_data, 'start_date'))
        {
            $this->filters['start_date']['value'] = MY::parseDate($input_data['start_date'])->startOfDay();
        }
        else
        {
            $this->filters['start_date']['value'] = now()->subMonth()->startOfDay();
        }

        if (Arr::get($input_data, 'end_date'))
        {
            $this->filters['end_date']['value'] = MY::parseDate($input_data['end_date'])->endOfDay();
        }
        else
        {
            $this->filters['end_date']['value'] = now()->endOfDay();
        }
    }

Example where we filter by a date range and by default the value is -1 month until today.

Controller

The controller and the routing is managed by the developer. You have to make sure there's a route linking to the new report. In that controller/route, in order to load the report all you have to do is something like this:

use Bernadev\LaravelReports\Loader;

public function generate($report, Request $request)
{
   $report = Loader::load($report, $request->all());

   $view_data['report'] = $report;
   $view_data['results'] = $report->generate();

   return view($report->getView(), $view_data);
}
  1. Load the report by passing the "slug" field (set when creating the report).
  2. Pass the report object and the results to the view
  3. Render the view. We can use the getView() method to retrieve its location.

About

Scaffolding to generate data reports in a unified way

Resources

License

Stars

Watchers

Forks

Packages

No packages published