KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Laravel Models with Relationships #2932

Open april bigby opened this topic on on Jan 8, 2023 - 2 comments

april bigby commented on Jan 8, 2023

I want to use Koolreport to write reports on an existing Laravel application. I would like to use the Models to grab the data instead of directly using sql bc the models have business logic that I do not want to repeat.

Using the laravel package for koolReport i can get basic reports working. My problem comes when i have a model that has relationships and I want to report on any of the child data. For example, I have a class called Customer. Customer has a relationships with Payments. I would like to sum the payments in a report. I have tried several ways to refer to the sum of the payments but i have not been able to get anything to work. Is there an example of how this would work?

thank u.

Adam commented on Sep 13

Bump, I'm also having an issue using my eloquent model relation(s) in koolreports dataSources

$this->src("elo")->query(Assignment::with('subjects:id,voter_number_state,voter_number_county,voter_status,first_name,middle_name,last_name')->where('reviewed_opinion', 3)
   ->groupBy('subject_id')
   ->select(['id', 'subject_id', 'reviewed_opinion'])
 )
 ->pipe(new Limit(array(10)))
 ->pipe($this->dataStore("evidence-package"));

The table is only picking up the "assignment" data but the query is returning the relation correctly ie $assignment->subjects. It looks like Eloquent->start() doesn't handle relations

Sebastian Morales commented on Sep 17

By default, our \koolreport\laravel\Eloquent data source class uses ->cursor() method to retrieve data to save memory usage for large datasets. Unfortunately, one limit of cursor method is that it doesn't load relationships:

Since the cursor method only ever holds a single Eloquent model in memory at a time, it cannot eager load relationships. If you need to eager load relationships, consider using the lazy method instead.

One solution you can apply right now is creating a sub class of \koolreport\laravel\Eloquent such as MyEloquent and replace its start() method with this:

class MyEloquent extends \koolreport\laravel\Eloquent
{
    public function start()  
    {
        $metaData = null;
        foreach($this->builder::lazy() as $item)
        {
            if(!$metaData)
            {
                $metaData = array(
                    "columns"=>array()
                );
                foreach($item->toArray() as $k=>$v)
                {
                    $metaData["columns"][$k] = array(
                        "type"=>Utility::guessType($v)
                    );
                }
                $this->sendMeta($metaData, $this);
                $this->startInput(null);
            }
            $this->next($item->toArray(), $this);
        }
        $this->endInput(null);
    } 

We will add this retrieval method as an option for the future releases of KoolReport.

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
None yet

Laravel