Thursday, April 13, 2017

Add lists() method in Query Builder in Laravel 5.4


I know the fact that Laravel has removed lists() function and moved functions signature as pluck(). But, this leads to a lot of work for someone who wants to Upgrade from Laravel 4.x to Laravel 5.4.

Hence, I am trying to find a way to just make use of existing function i.e. lists() in my code and just make use of pluck()->toArray() when this function is called.


I have tried the following.

Method 1

class BaseModel extends  Illuminate\Database\Query\Builder
public function __call($method, $args)
{
    return call_user_func_array($this->method,$args);
}

public function lists($column){
return $this->pluck($column)->toArray();
}

Wont work!.
Reason : This needs to be extended along with the BaseModel class. But, it already extends Eloquent Model Class.

Method 2

Tried adding the Required function using trait like

listsWorkAround.php

<?php
trait listsWorkAround
{ 
  function lists($column){
    return $this->pluck($column)->toArray();
  }
}

Model.php

<?php
namespace App;

use Watson\Rememberable\Rememberable;
use Illuminate\Database\Eloquent\Model as Eloquent;

abstract class Model extends Eloquent
{
    use listsWorkAround;
    use Rememberable;
}

Nope ain't no success.

Method 3

Tried adding a as ServiceProvider and add a macro function for the Builder Class i.e. lists in this case.
But, the problem is the final returned entity is a Collection no matter what as it is returned using the __call function of the Builder. But, the desired entity is an Array.



via Ateeq Ahmed

Advertisement