Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Controller / Model with PHPSpreadSheet

Hello,

I have a controller to generate a customized Excel spreadsheet.

But the method is very big (set titles, customize the cells, ...).

And I have to generate in the same sheet three same tables with different datas.

So to use three times the same code, I want to dispatch the code in methods.

  • I have tried to add other methods in the controller and access to them using $this-> but Laravel generates an error saying that it's not a model context.

  • I wanted to create a model (not extended from Laravel Model) which extends PHPSpreadSheet class to be able to use the PHPSpreadSheet methods and my methods, I have tried, but Laravel generates an error saying that the spreadsheet methods are undefined.

How is it possible to extend a model from another external model in Laravel to be able to reuse the external model methods ?

Well ... any idea is welcome ;).

Thanks a lot.

V

0 likes
3 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

In your new model, or service class, have use statements for the models needed. Otherwise the data is going to be unavailable.

I do similar sometimes, have another class to work out a bunch of where clauses, so a method in a model doesn't get overly bloated, and just use a return statement.

1 like
jlrdw's avatar

Good it's working, yes I've had to experiment around sometimes when extending versus implementing, etc.

2 likes

Please or to participate in this conversation.