【问题标题】:foreach loop with emply items Laravel带有空项目的 foreach 循环 Laravel
【发布时间】:2017-09-27 19:13:31
【问题描述】:

这是我的循环:

 $azienda = Azienda::where('id', $id_a)->get();
        foreach($azienda as $az){
            $id_azienda = $az->id; //code } 

$azienda 有空项目,这会停止循环...

如何继续循环并跳过空项目?

dd($aziendas->toArray());

    array:1 [▼
  0 => array:16 [▼
    "id" => 1
    "titolo" => "Studio Rossi"
    "categoria" => 1
    "logo" => "logonull.png"
    "descrizione" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent pharetra,  n ▶"
    "indirizzo" => "via larga 1"
    "provincia_id" => 2
    "comune" => "Roma"
    "telefono" => "06 555444"
    "mobile" => "333 456789"
    "sitoweb" => "www.ldoksid.it"
    "email" => "ls*****@gmail.com"
    "remember_token" => null
    "created_at" => null
    "updated_at" => "2017-09-27 16:13:57"
    "approved" => 0]]

【问题讨论】:

    标签: laravel foreach


    【解决方案1】:

    我假设当您说 has empty items 时,您的意思是模型实际上是空的 - 没有使用数据库中的记录进行水合 - 在这种情况下,您可以过滤记录以仅获取实际具有水合模型的记录:

    $aziendas = Azienda::where('id', $id_a)->get();
    
    $aziendas->filter(function(Azienda $azienda) {
        return $azienda->exists;
    });
    

    如果集合中每个模型的实例上的某些属性可能为空 - 只需替换闭包中的逻辑,即

    $aziendas = Azienda::where('id', $id_a)->get();
    
    $aziendas->filter(function(Azienda $azienda) {
        return !empty($azienda->name);
    });
    

    或者如果它的关系可能没有任何关联记录,即Post没有任何Comment,那么您可以将其检查为:

    $aziendas = Azienda::with('comments')->where('id', $id_a)->get();
    
    $aziendas->filter(function(Azienda $azienda) {
        return $azienda->comments->count() > 0;
    });
    

    请注意我 Eager loaded comments 使用 with('comments') 以确保我只向数据库发出 2 个请求,而不是每次迭代都请求。显然,您可能与comments 有不同的关系 - 只需将其替换为相关关系,然后在过滤器闭包中使用关系方法的名称作为属性来计算关联记录,并仅返回在该关系上实际具有任何关联记录的记录。

    您也可以使用whereHas() eloquent 方法仅获取具有特定关系记录的记录 - 这可能是一个更好的选择:https://laravel.com/docs/5.5/eloquent-relationships#querying-relationship-existence

    $aziendas = Azienda::whereHas('comments')->where('id', $id_a)->get();
    

    【讨论】:

    • 数据库中的记录是不连续的,所以对于item id 1,item id 2循环继续,然后因为item id 3不存在,而是item id 13而停止。
    • hmm - 如果 Item 不存在,那么 eloquent 如何返回它?我真的不确定doesn't exist 是怎么回事 - 你可以试试和dd($aziendas->toArray()) 并将它们添加到你的票中,这样我就可以确切地看到你从这个查询中得到了什么?
    • 是一个数据透视表...所以我有 id 1,2,3 id_azienda 1,2,13 和 id_cliente 2,3,5
    • 所以你想知道这种关系是否存在?如果是这样,您可以使用关系方法名称作为属性来检查这种关系是否存在。确保您急切地加载它们,以确保您不会在每次迭代时对数据库运行查询。更多信息在这里:laravel.com/docs/5.5/eloquent-relationships#eager-loading
    【解决方案2】:

    快速回答是使用continue。这可以按如下方式完成:

    $azienda = Azienda::where('id', $id_a)->get();
            foreach($azienda as $az){
                $id_azienda = $az->id; //code 
                if (is_null($az) { continue; }
            } 
    

    不过,我也建议您利用 Laravel 的 collection methods。有一个each 方法:

    $azienda->each(function ($item, $key) {
        // ..
    });
    

    【讨论】:

    • 如何继续使用?
    • 为什么要降级?
    【解决方案3】:

    要获取 id,您可以使用 pluck 方法。

    $azienda = Azienda::where('id', $id_a)->get();
    
    $ids = $azienda->pluck('id')->toArray();
    

    更多信息:https://laravel.com/docs/5.5/collections#method-pluck

    如果你只想中断一个循环,你可以使用'break'。

    foreach ($ids as $id) {
     if($something..) {
        break;
     }
    }
    

    更多信息:http://php.net/break

    【讨论】:

      猜你喜欢
      • 2017-05-20
      • 2021-04-27
      • 1970-01-01
      • 2016-11-30
      • 2020-03-08
      • 2018-02-27
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多