Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在收藏中搜索laravel_Php_Laravel 5 - Fatal编程技术网

Php 在收藏中搜索laravel

Php 在收藏中搜索laravel,php,laravel-5,Php,Laravel 5,我收集了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: 1

我收集了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',即使该值确实存在。

first其中:

$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;
第一,其中:

$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;
拉威尔4.2
firstWhere
(不工作)

首先:

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

$needle = 8;
$exists = $items->filter(function($key, $item) use ($needle) { 
      return $item->emp_id === $needle && $item->dst_id === $needle ;
 })->count() > 0;
拉威尔4.2
firstWhere
(不工作)

首先:

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

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

您可以在
collections
上使用
where方法,如下所示:

$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();

您可以在
collections
上使用
where方法,如下所示:

$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();
只用

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

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

你确定第一个例子有效吗?我正在尝试
$this->permissions->where(['user\u id'=>$user->id])
但它不起作用,但确实起作用
$this->permissions->where('user\u id',$user->id)
。文档中也没有提及数组选项。如果您查看Collection.php中的
操作符forwhere()
方法,我看不出您的示例可能如何工作。您确定第一个示例可以工作吗?我正在尝试
$this->permissions->where(['user\u id'=>$user->id])
但它不起作用,但确实起作用
$this->permissions->where('user\u id',$user->id)
。文档中也没有提及数组选项。如果您查看Collection.php中的
操作符forwhere()
方法,我看不出您的示例可能如何工作。