Laravel Eloquent \ Collection class extension

I understand that the default Eloquent\Collection class can be overridden in your model using the method:

 public function newCollection(array $models = array()) { return new CustomCollection($models); } 

This works fine if I use typical queries, for example:

 Model::where('name', $name)->get(); 

This is great, so I can add methods to the eloquent collection class, for example:

 $records = Model::where('name', $name)->get(); $records->toTable(); 

But if I use pagination on a model, for example:

 Model::where('name', $name)->paginate(25); 

It returns an instance of the Illuminate\Support\Collection class instead of Illuminate\Database\Eloquent\Collection .

Is there a way to override or extend a typical Illuminate\Support\Collection ?

I am trying to add the toTable() method to the returned collection. I would rather not replace the pagination service provider with my own.

Thanks!!

+5
source share
1 answer

You will need to replace the pagination service provider, among several other classes in the pagination library. By the sound of this, you know how to do it, but you hoped for a different answer, but since I have the code, I will pick it up here for you.

The reason you need to replace these classes / methods is because the files in Illuminate directly reference class instances in the Illuminate namespace.

In config / app.php

Replace

 'Illuminate\Pagination\PaginationServiceProvider', 

FROM

 'ExtendedPaginationServiceProvider', 

Create a new file somewhere autoloader is able to find it called ExtendedPaginationServiceProvider.php and put the following into it

 <?php use Illuminate\Support\ServiceProvider; class ExtendedPaginationServiceProvider extends ServiceProvider { /** * @inheritdoc */ public function register() { $this->app->bindShared('paginator', function($app) { $paginator = new ExtendedPaginationFactory($app['request'], $app['view'], $app['translator']); $paginator->setViewName($app['config']['view.pagination']); $app->refresh('request', $paginator, 'setRequest'); return $paginator; }); } } 

Create a new file somewhere autoloader can find it called ExtendedPaginationFactory.php and put the following in it

 <?php use Illuminate\Pagination\Factory; class ExtendedPaginationFactory extends Factory { /** * @inheritdoc */ public function make(array $items, $total, $perPage = null) { $paginator = new ExtendedPaginationPaginator($this, $items, $total, $perPage); return $paginator->setupPaginationContext(); } } 

Create a new file somewhere the autoloader can find it called ExtendedPaginationPaginator.php and put the following into it

 <?php use Illuminate\Pagination\Paginator; class ExtendedPaginationPaginator extends Paginator { /** * Get a collection instance containing the items. * * @return ExtendedCollection */ public function getCollection() { return new ExtendedCollection($this->items); } } 

You will notice that the above returns a new instance of ExtendedCollection. Obviously replace this with your CustomCollection class, which you are referring to in your question.

To refer to others, the ExtendedCollection class might look something like the one below.

Create a new file somewhere autoloader will be able to find it with the name ExtendedCollection.php and put the following into it

 <?php use Illuminate\Support\Collection; class ExtendedCollection extends Collection { } 

In addition, after creating these files do not forget to run in the terminal

following:
 composer dump-autoload 
+6
source

Source: https://habr.com/ru/post/1207230/


All Articles