Laravel Collection mapToDictionary 例子 Posted on 2019-01-04 Edited on 2022-06-15 源码 x 示例 12345678910111213141516171819202122232425262728<?php require __DIR__ . '/bootstrap/app.php'; $arr = [ [ 'name' => 'John', 'age' => 23 ], [ 'name' => 'Neo', 'age' => 25 ], [ 'name' => 'John', 'age' => 24 ]]; $v = collect($arr)->mapToDictionary(function ($v) { // 主要作用: 可以自定义每一项的 key value return [$v['name'] => $v];})->toArray(); // $v 与 $v1 相等$v1 = collect($arr)->groupBy('name')->toArray(); dd($v); 输出 x
Laravel mapSpread 例子 Posted on 2019-01-04 Edited on 2022-06-15 1234567891011121314151617$collection = collect(range(1, 9)); $chunks = $collection->chunk(2); $labeld = $chunks->mapSpread(function ($odd, $even) { return "Odd: {$odd} Even: {$even}";}); $numbers = collect([3, 5, 7]);$words = collect(['three', 'five', 'seven']); $results = $numbers->zip($words) ->mapSpread(function ($number, $word) { return $number . '=' . $word; }); dd($labeld->toArray(), $results->toArray()); x
Laravel DB listen 回调追踪产生 sql 语句的代码 Posted on 2018-12-24 Edited on 2020-12-08 1234\DB::listen(function (QueryExecuted $sql) { \Log::info($sql->sql); \Log::info((new \Exception())->getTraceAsString());});
Lumen 使用 laravel-cors 的时候, 使用 dd 函数的解决方法 Posted on 2018-12-14 Edited on 2020-12-08 123456789101112131415161718192021if (! function_exists('dd')) { /** * Dump the passed variables and end the script. * * @param mixed $args * @return void */ function dd(...$args) { header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: *'); header('Access-Control-Allow-Headers: *'); http_response_code(500); foreach ($args as $x) { (new Illuminate\Support\Debug\Dumper)->dump($x); } die(1); }}
laravel Syntax error or access violation 1071 Specified key was too long max key length is 767 bytes (SQL alter table users add unique users_email_unique(email) Posted on 2018-11-22 Edited on 2020-12-08 AppServiceProvider 123456use Illuminate\Support\Facades\Schema; public function boot(){ Schema::defaultStringLength(191);}