laravel 集合接口

只记下几个经常使用的,其余的看这里:http://laravelacademy.org/post/6293.htmlhtml

 

1)什么是集合?laravel

就是laravel查询构建器查询返回的数据结果(get first find等),它是 Illuminate\Support\Conllection 的一个实例,除了查询返回,咱们也能够使用助手函数 collect 生成一个 实例,例如 $collection = collect([1,2,3,4]);函数

 

2)判断查询结果是否为空(假设查询结果为$result,下文延用)post

if(!$result->isEmpty())
{
    //非空
}
else
{
   //
}

 

3)统计结果总数:$result->count();spa

 

4)判断给定的是否存在:$result->has('key_name');code

 

5)判断给定的是否存在:$result->contains('value');htm

 

6)取出集合第一个元素:$result->first(),相应的还有返回最后一个元素的方法:$result->last();blog

 

7)经过键名直接取出相应数据:$result->get(0) 或 $result->get('name');排序

 

8)删除一个元素:$result->forget(0) 号 $result->forget('name');get

 

9)排序:$result->sort(), $result->sortBy('price');

 

10)最大值、最小值:$result->max(), $result->mix();

 

11)合集、差集:merge、diff

$collection = collect(['product_id' => 1, 'name' => 'Desk']);
$merged = $collection->merge(['price' => 100, 'discount' => false]);
$merged->all();
// ['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]
$collection = collect([1, 2, 3, 4, 5]);
$diff = $collection->diff([2, 4, 6, 8]);
$diff->all();
// [1, 3, 5]

 

 

更多,请看这:http://laravelacademy.org/post/6293.html

相关文章
相关标签/搜索