【问题标题】:Laravel 6.x working with Eloquent and arrays - avoid queries inside foreachLaravel 6.x 使用 Eloquent 和数组 - 避免在 foreach 中查询
【发布时间】:2020-04-12 03:25:26
【问题描述】:

您好,我的应用程序中的 foreach 循环中有很多 Eloquent 查询,我正在尽可能避免这种做法。

例子:

$statuses = Statuses::get(); // or Statuses::all(); // What's the difference because I use them mixed?

$statusList = [];

foreach ($statuses as $status) {
    $statusList[$status->id] = $status->status_title;
} // How do I just get my array of the id => status_title in single Eloquent query?

// Return $statusList to use in my select drop-down...

我有更多的领域我正在使用 foreach 来获得我想要的东西,在其中一些我可能会查询 10 到 100 次 - 这是我现在根据不同的 Stack Overflow 答案尝试的内容:

$statuses = $statuses->map->only(['id', 'status_title']);

但这并没有给我我需要的数组格式,我需要一维 [id] => [title] 但是那个给我列名。来源:Get only specific attributes with from Laravel Collection

谢谢

谢谢!尝试这个给了我主键作为数组键和状态标题作为没有列名的值。抱歉,我刚刚意识到这个示例在 foreach 中没有查询。

我还有一个不知道该怎么做的问题:现在我想连接一些列,例如:

$statusList = Status::pluck('status_title', 'status_outcome', 'id')); 
$array = $statusList->all();
what I'm trying to do is [id] => [status_title . ' ' . status_outcome] 

所以基本上我将数组键作为主键 ID,将值作为连接的标题和结果?

请帮助我在使用 ->pluck() 方法时遇到问题:

Statuses::select("CONCAT('status_title', ' - ', 'status_outcome') AS status, id")->pluck('status', 'id')->all(); 

尝试获取数组但 pluck() 在我的不同查询中表现不同,有时我会收到错误。我也试图采摘超过 2 列它会给出错误,例如

Statuses::pluck('status_title', 'status_outcome', 'id')->all() I also tried ->get() and ->toArray()

【问题讨论】:

    标签: laravel eloquent laravel-6 laravel-6.2


    【解决方案1】:

    这就是pluck 方法的用途:

    $statusList = Status::pluck('status_title', 'id');
    

    这将为您提供一个 Collection,以 'id' 为键,并以 'status_title' 作为值。如果您想将该 Collection 转换为 array,请在 Collection 上调用 all()

    $array = $statusList->all();
    

    Laravel 6.x Docs - Queries - Retrieving Results - Retrieving A List Of Column Valuespluck

    您所拥有的也不是循环中的任何查询。您只查询一次,然后在您的示例中迭代结果。


    如果你需要使用一些功能(不是所有支持的DB都有concat):

    $list = Status::selectRaw("concat(status_title, ' - ', status_outcome) as con, id")
        ->pluck('con', 'id');
    

    【讨论】:

    • 一行应该是:$statuses = Statuses::all()->pluck('status_title', 'id');
    • @koalaok ...不是真的,这会提取所有记录,将这些模型与所有属性水合,然后遍历它们以获得这两个字段...这只会选择需要的字段而不是水合任何模型,发生了非常不同的事情,这是在 Builder 上调用 pluck,而不是 Collection
    • 我想他想要所有的记录……也许他可以只选择想要的列。但我不确定这是否会提高性能。
    • 不,这不是他们的代码所说的,但也许他们需要更多数据,必须查看
    • 谢谢!尝试这个给了我主键作为数组键和状态标题作为没有列名的值。抱歉,我刚刚意识到这个示例在 foreach 中没有查询。我还有一个不知道该怎么做的问题:现在我想连接一些列,例如:$statusList = Status::pluck('status_title', 'status_outcome', 'id')); $statusList->all()。我想做的是 [id] => [status_title 。 ' ' 。 status_outcome] 所以基本上我将数组键作为主键 ID,将值作为连接的标题和结果?
    【解决方案2】:

    使用 mysql concat() 和 pluck() Laravel Helper 可以达到你想要的效果:

    $statuses = Statuses::select("CONCAT('status_title', ' ', 'status_outcome') AS status, id")->get()->pluck('status', 'id');
    

    【讨论】:

    • 您好,现在将连接的字符串作为值,键作为 id。只是在这里进行健全性检查,这个输出是一个数组吗?是吗?
    • 请帮助我在使用 ->pluck() 方法时遇到问题:Statuses::select("CONCAT('status_title', ' - ', 'status_outcome') AS status, id")->pluck('status', 'id')->all(); 尝试获取数组,但 pluck() 在我的不同查询中表现不同,有时我会收到错误消息。我也试图采摘超过 2 列它会给出错误,例如Statuses::pluck('status_title', 'status_outcome', 'id')->all() I also tried ->get() and ->toArray()
    猜你喜欢
    • 1970-01-01
    • 2019-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多