Extending the Laravel Eloquent\Collection Class -


i understand default eloquent\collection class can overridden in model using method:

public function newcollection(array $models = array()) {      return new customcollection($models); } 

which works great if i'm using typical queries such as:

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

this great can add methods eloquent collection class, such as:

$records = model::where('name', $name)->get();  $records->totable(); 

but if i'm using pagination on model, example:

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

it returns instance of class illuminate\support\collection instead of illuminate\database\eloquent\collection.

is there way of overriding or extending typical illuminate\support\collection?

i'm trying add totable() method returned collection. i'd rather not have replace pagination service provider own.

thanks!!

you need replace pagination service provider, amongst couple of other classes in pagination library. sound of know how way, hoping answer, have code i'll drop in here you.

the reason need replace these classes/methods because files in illuminate directly reference instances of classes within illuminate namespace.

in config/app.php

replace

'illuminate\pagination\paginationserviceprovider', 

with

'extendedpaginationserviceprovider', 

create new file somewhere autoloader capable of finding called extendedpaginationserviceprovider.php , place following in 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 new file somewhere autoloader capable of finding called extendedpaginationfactory.php , place 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 new file somewhere autoloader capable of finding called extendedpaginationpaginator.php , place following in it

<?php  use illuminate\pagination\paginator;  class extendedpaginationpaginator extends paginator {     /**      * collection instance containing items.      *      * @return extendedcollection      */     public function getcollection()     {         return new extendedcollection($this->items);     } } 

you'll notice above returns new instance of extendedcollection. replace customcollection class refer in question.

for others reference, extendedcollection class may similar below

create new file somewhere autoloader capable of finding called extendedcollection.php , place following in it

<?php  use illuminate\support\collection;  class extendedcollection extends collection {  } 

also, after creating these files, don't forget run following in terminal

composer dump-autoload 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -