* Create a new collection with a Collection class instance. */ public function classCollection() { $newCollection = new Collection([1, 2, 3, 4, 5]); dd($newCollection); } } 这个帮助函数用起来要简单很多因为你再不需要实例化Illuminate\
例如:$collection->first(function ($value, $key) { return ! is_null($value);});在Laravel 之前的版本中,$key 被作为第一个参数。由于大多数情况下只需要用到 $value,所以现在我们把它作为第一个参数。你应该在应用程序中做一次「全局搜索」,以确保 $value 是第一个被传递到闭包的参数。
*/public function contains(){$users = User::all();$users->contains('name', 'Chasity Tillman');//true$collection = collect(['name' => 'John', 'age' => 23]);$collection->contains('Jane');//false$collection = collect([1, 2, 3, 4, 5]);$collection->contains(function ($key, $...
再者,我们还需要借助临时变量以及 PHP 中内置的不友好的 sort 方法。 现在,让我们看下借助 Collection 类实现起来是多么简单吧: collection($data)->where('age', 'thirties') ->sortBy('last_name') ->map(function($item){ return $item['first_name'].' '.$item['last_name']; }) ->implode("\...
The forget method removes an item from the collection by its key:1$collection = collect(['name' => 'taylor', 'framework' => 'laravel']); 2 3$collection->forget('name'); 4 5$collection->all(); 6 7// ['framework' => 'laravel']...
$collection = collect([1, 2, 3, 4, 5]);$collection->contains(function ($value, $key) { return $value > 5;});// falsecontains 方法在检查项目值时使用「宽松」比较,意味着具有整数值的字符串将被视为等于相同值的整数。 相反 containsStrict 方法则是使用「严格」比较进行过滤。
1$collection = collect(['name' => 'taylor', 'framework' => 'laravel']); 2 3$flipped = $collection->flip(); 4 5$flipped->all(); 6 7// ['taylor' => 'name', 'laravel' => 'framework']forget()The forget method removes an item from the collection by its key:...
I commonly find that there is no shorthand way of sorting collections by multiple fields. A large amount of extra code is required just by adding in an extra field. What I really want to do is this: $collection->sortBy(['Surname', 'Foren...
protected $primaryKey = 'uuid'; // 更换主键 public $incrementing = false; // 设置 不自增长 protected $perPage = 25; // 定义分页每页显示数量(默认15) const CREATED_AT = 'created_at'; const UPDATED_AT = 'updated_at'; //重写 时间字段名 ...
protected $primaryKey = 'uuid'; // 更换主键 public $incrementing = false; // 设置 不自增长 protected $perPage = 25; // 定义分页每页显示数量(默认15) const CREATED_AT = 'created_at'; const UPDATED_AT = 'updated_at'; //重写 时间字段名 ...