Laravel 5.4 查询生成器中添加lists() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43396489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:47:15  来源:igfitidea点击:

Add lists() method in Query Builder in Laravel 5.4

phplaraveleloquentlaravel-query-builderpluck

提问by Ateeq Ahmed



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.

我知道 Laravel 已删除lists()函数并将函数签名移动为pluck(). 但是,对于想要从Laravel 4.x升级到 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.

因此,我试图找到一种方法来仅使用现有函数,即lists()在我的代码中,并在pluck()->toArray()调用此函数时使用。


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.

行不通!
原因:这需要与 BaseModel 类一起扩展。但是,它已经扩展了 Eloquent 模型类。

Method 2

方法二

Tried adding the Required function using traitlike

尝试使用trait类似添加必需的功能

listsWorkAround.php

列出WorkAround.php

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

Model.php

模型.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 ServiceProviderand add a macrofunction for the BuilderClass i.e. listsin this case.
But, the problem is the final returned entity is a Collection no matter what as it is returned using the __callfunction of the Builder. But, the desired entity is an Array.

在这种情况下,尝试添加一个 asServiceProvidermacroBuilder类添加一个函数lists
但是,问题是最终返回的实体是一个集合,不管它是使用__callBuilder的函数返回的。但是,所需的实体是Array.

Edit : The Code I used for Method 3

编辑:我用于方法 3 的代码

<?php
namespace Providers;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\ServiceProvider;

class ListsWorkAround extends ServiceProvider
{
    /**
     * {@inheritdoc}
     */
    public function register()
    {
        Builder::macro("lists", function ($column) {        
            return $this->pluck($column)->toArray();
        });
    }
}

But, as I said this would still return Collection.

但是,正如我所说,这仍然会返回 Collection。

回答by Ateeq Ahmed

So, this is what I ended up doing to make it work i.e. lists()in Model::XXX->lists()

所以,这是我落得这样做,使其工作,即lists()Model::XXX->lists()

Added this in ..../config/app.php

..../config/app.php 中添加了这个

'providers' => [
 ...
 ...
 \App\Providers\ListsWorkAround::class,
],

and this is Provider Class File.

这是提供者类文件。

ListsWorkAround.php

ListsWorkAround.php

<?php
/**
 * Created by PhpStorm.
 * User: ateeq-ahmed
 * Date: 14/4/17
 * Time: 11:25 AM
 */

namespace App\Providers;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\ServiceProvider;

class ListsWorkAround extends ServiceProvider
{
    /**
     * {@inheritdoc}
     */
    public function register()
    {
        Builder::macro("lists", function ($column, $key = null) {
            return $this->pluck($column, $key)->all();
        });

        QueryBuilder::macro("lists", function ($column, $key = null) {
            return $this->pluck($column, $key)->all();
        });
    }
}

回答by lagbox

If you really wanted to you could macro listsinto Query Builder, Eloquent Builder and Collection

如果你真的想要,你可以宏lists到 Query Builder、Eloquent Builder 和 Collection

Macro'ing Query Builder:

宏查询生成器:

Illuminate\Database\Query\Builder::macro('lists', function ($column, $key = null) {
    return $this->pluck($column, $key)->all(); 
});


DB::table('users')->lists('email');