【问题标题】:search in collection laravel在集合中搜索 laravel
【发布时间】:2018-01-22 16:28:39
【问题描述】:

我有一个非常大的 150 个对象的集合

 ...
 App\almacen {#1679
     id: 124,
     emp_id: 1,
     dst_id: 13,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1680
     id: 125,
     emp_id: 1,
     dst_id: 11,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1681
     id: 126,
     emp_id: 1,
     dst_id: 12,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1682
     id: 127,
     emp_id: 1,
     dst_id: 20,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
   App\almacen {#1683
     id: 128,
     emp_id: 1,
     dst_id: 7,
     hora: 0,
     numMesa: 0,
     event_id: 1,
     created_at: "2018-01-22 11:41:03",
     updated_at: "2018-01-22 11:41:03",
   },
  ...

我必须比较键 emp_id 和 dst_id 是否等于数字,例如 8,如果存在则返回“true” 有没有办法在不使用“foreach”并直接查看对象的情况下做到这一点? 已经使用 contains(), search() 但即使值确实存在也总是返回 'false'

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:

    您可以像这样在collections 上使用where method

    $collection = collect([
        ['product' => 'Desk', 'price' => 200],
        ['product' => 'Chair', 'price' => 100],
        ['product' => 'Bookcase', 'price' => 150],
        ['product' => 'Door', 'price' => 100],
    ]);
    
    $filtered = $collection->where('price', 100);
    
    $filtered->all();
    

    【讨论】:

      【解决方案2】:

      第一地点:

      $exists = $items->firstWhere(['emp_id' => 8, 'dst_id' => 8]) !== null;
      

      第一:

      $needle = 8;
      $exists = $items->first(function($item) use($needle) { 
            return $item->emp_id === $needle && $item->dst_id === $needle ;
       }) !== null;
      

      过滤器:

      $needle = 8;
      $exists = $items->filter(function($item) use($needle) { 
            return $item->emp_id === $needle && $item->dst_id === $needle ;
       })->count() > 0;
      

      【讨论】:

      • 你确定第一个例子有效吗?我正在尝试$this->permissions->where(['user_id' => $user->id]),但它不起作用,但这确实有效$this->permissions->where('user_id', $user->id)。文档中也没有提到数组选项。如果您查看 Collection.php 中的 operatorForWhere() 方法,我看不出您的示例可能如何工作。
      【解决方案3】:

      Laravel 4.2

      firstWhere(不工作)

      first:

      $needle = 8;
      $exists = $items->first(function($key, $item) use ($needle) { 
            return $item->emp_id === $needle && $item->dst_id === $needle ;
       }) !== null;
      

      filter

      $needle = 8;
      $exists = $items->filter(function($key, $item) use ($needle) { 
            return $item->emp_id === $needle && $item->dst_id === $needle ;
       })->count() > 0;
      

      【讨论】:

        【解决方案4】:

        随便用

        return $collection->where('price', '100')->toArray();
        

        【讨论】:

          猜你喜欢
          • 2019-05-15
          • 2020-12-12
          • 1970-01-01
          • 1970-01-01
          • 2017-09-04
          • 2019-03-10
          • 2016-02-12
          • 2020-07-06
          • 1970-01-01
          相关资源
          最近更新 更多