Reports

Defining Reports

Lamotivo reports allow you to customize the underlying resource query, you are free to join additional tables and perform aggregate functions within the query.

To get started, you may use the lm:report Artisan command. By default, Lamotivo will place newly generated reports in the app/Lm/Reports directory:

php artisan lm:report ProductSales

Each report generated by Lamotivo contains several methods. The two methods are the query and fields methods. The query method is responsible for building the Eloquent query that is needed to retrieve the desired data, while the fields method returns an array of fields that should be displayed in the report.

To learn more, let's take a look at a complete report definition that displays products and their revenue:

<?php

namespace App\Lm\Reports;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use Anyspin\Lamotivo\Report;
use Anyspin\Lamotivo\Fields\ID;
use Anyspin\Lamotivo\Fields\Text;
use Anyspin\Lamotivo\Fields\Number;
use Anyspin\Lamotivo\Fields\Currency;

class ProductSales extends Report
{
    /**
     * The title of the report.
     *
     * @var string
     */
    public static $title = 'Product Sales';

    /**
     * The slug of the report.
     *
     * @var string
     */
    public static $slug = 'product-sales';

    /**
     * Execute the query for the report.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Database\Eloquent\Builder  $query
     * @return mixed
     */
    public function query(Request $request, $query)
    {
        return $query->select([
            'products.id',
            'products.name',
            DB::raw('count(*) as orders_count'),
            DB::raw('sum(orders.cost) as revenue'),
          ])
          ->join('orders', 'products.id', '=', 'orders.product_id')
          ->orderBy('orders_count', 'desc')
          ->groupBy('orders.product_id');
    }

    /**
     * Get the fields available for the report.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make(),
            Text::make('name', 'Name'),
            Number::make('orders_count', 'Order Count'),
            Currency::make('revenue', 'Revenue'),
        ];
    }
}

As you can see in the example above, the query method has full control of the Eloquent query used to retrieve the report data.

Report Metrics

Each Lamotivo report also contains a cards method. This method allows you to attach any available metrics to the report:

use App\Lm\Metrics\NewSales;

/**
 * Get the cards available for the report.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function cards(Request $request)
{
    return [
        new NewSales,
    ];
}

Report Filters

Each Lamotivo report also contains a filters method. This method allows you to attach any available filters to the report:

use App\Lm\Filters\ProductType;

/**
 * Get the filters available for the report.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function filters(Request $request)
{
    return [
        new ProductType,
    ];
}

Report Actions

Each Lamotivo report also contains an actions method. This method allows you to attach any available actions to the report:

use App\Lm\Actions\CsvExport;

/**
 * Get the actions available for the report.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function actions(Request $request)
{
    return [
        new CsvExport,
    ];
}

Registering Reports

Once you have defined a report, you are ready to attach it to a Lamotivo resource. Each resource generated contains a reports method. To attach a report to a resource, you should simply add it to the array of reports returned by this method:

/**
 * Get the reports available for the resource.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
public function reports(Request $request)
{
    return [
        new Reports\ProductSales,
    ];
}