Laravel – Is it possible to access controller function in eloquent?

so I have this one table in view where all the columns and rows are displaying a mathematical calculation. Every calculation were calculated in controller which is not store in the database.

The problem is I need to do an excel export based on the table display in view. I’m using phpspreadsheet and I’m writing the code in eloquent. I want to get all the variable in the controller and display in the excel code in my eloquent. Is it possible to do that?

Fyi, the controller with all the mathematical code was coded by previous developer, I was asked to added export excel function only which I’m not sure how to get all the method inside the controller.

I already did the export excel but only on getting all data that exist in the database which is easier because all I need is query = Model::all();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Without seeing the code you are talking about, Here is what I mean by moving the calculation and modification of the data from the controller to a static method in the model.

SomeController.php original:

<?php

namespace AppHttpControllers;

class SomeController extends Controller
{
    public function index()
    {
        $table_data = collect(SomeModel::all())->map(function($item) {
            // your funky table data stuff.
            return $item;
        })->reduce(function($carry, $item) {
            // your funky table data stuff.
            return $carry + $item;
        });

        return view('some-table-view', $table_data);
    }
}

Move the logic from your controller to the Model.

SomeModel.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class SomeModel extends Model
{
    ...

    public static function tableData()
    {
        return collect(self::all())->map(function($item) {
            // your funky table data stuff.
            return $item;
        })->reduce(function($carry, $item) {
            // your funky table data stuff.
            return $carry + $item;
        });
    }
}

Your new SomeController.php:
<?php

namespace AppHttpControllers;

class SomeController extends Controller
{
    public function index()
    {
        $table_data = SomeModel::tableData();
        return view('some-table-view', $table_data);
    }
}

Now you can call SomeModel::tableData(); where its required.

Method 2

i think you can use datatable with serverside false
datatable with export button


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x