PHP Iterator(迭代器) 遍历值 作者: Chuwen 时间: 2020-12-04 分类: PHP 评论 # 有这么一个迭代器 ```php class myIterator implements Iterator { private $index = 0; private $data = ''; public function __construct($data) { $this->index = 0; $this->data = $data; } function rewind() { $this->index = 0; } function current() { return $this->data[$this->index]; } function key() { return $this->index; } function next() { ++$this->index; } function valid() { return isset($this->data[$this->index]); } } $it = new myIterator(array( "hello", "php", "iterator", )); ``` # 遍历取值 ## 1. while 循环♻️取值 ```php valid()){ echo "key: {$it->key()} , value: {$it->current()}".PHP_EOL; $it->next();//指向下一项 } ``` ## 2. foreach 取值 > 我们通过 `foreach` 遍历 `$it` 时,PHP 会自己依次调用: > > `rewind()` 重置到第一个元素 > `valid()` 检查当前位置是否有效 > `current()` 返回当前元素 > `key()` 返回当前元素的键 > `next()` 指向下一个元素 ```php foreach($it as $key => $value) { echo "$key : $value"; } ``` --- 部分摘抄自:https://segmentfault.com/a/1190000016475883
Laravel 安装指定版本 作者: Chuwen 时间: 2020-11-23 分类: PHP 评论 ```shell composer create-project --prefer-dist laravel/laravel blog "6.*" ```
Laravel 创建数据库遇到的问题:Illuminate\Database\QueryException : could not find driver (SQL: select * from 作者: Chuwen 时间: 2020-11-19 分类: Laravel,PHP 评论 # 执行以下命令 `php artisan migrate` # 报以下错误 ```log E:\PhpStorm_Project\learn-laravel>php artisan migrate Illuminate\Database\QueryException : could not find driver (SQL: select * from information_schema.tables where table_schema = laravel and table_name = migrations and table_typ e = 'BASE TABLE') at E:\PhpStorm_Project\learn-laravel\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669 665| // If an exception occurs when attempting to run a query, we'll format the error 666| // message to include the bindings with SQL, which will make this exception a 667| // lot more helpful to the developer instead of just the database's errors. 668| catch (Exception $e) { > 669| throw new QueryException( 670| $query, $this->prepareBindings($bindings), $e 671| ); 672| } 673| Exception trace: 1 PDOException::("could not find driver") E:\PhpStorm_Project\learn-laravel\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 2 PDO::__construct() E:\PhpStorm_Project\learn-laravel\vendor\laravel\framework\src\Illuminate\Database\Connectors\Connector.php:70 Please use the argument -v to see more details. ``` # 原因: 1. 你可能配置根目录下的 `.env` 文件,你应该像我这样配置 ```env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=laravel DB_PASSWORD=laravel ``` 2. 未启用 `pdo_mysql` 扩展 1. Linux:在 php.ini 文件加入这一行 `extension=pdo_mysql.so` 2. Windows:在 php.ini 文件加入这一行 `extension=pdo_mysql.dll`
Laravel 使用artisan 快速创建数据库表 作者: Chuwen 时间: 2020-11-19 分类: Laravel,PHP 评论 # 首先,项目根目录执行 > 下面表示要创建 `chuwen_user` 这个表 ```shell php artisan make:migration chuwen_user ``` 运行结果: ``` E:\PhpStorm_Project\learn-laravel>php artisan make:migration chuwen_user Created Migration: 2020_11_19_060051_chuwen_user ``` ### 你可以在 `项目根目录/database/migrations/2020_11_19_060051_chuwen_user.php` 找到刚刚生成的类 # 编辑表字段 > 更多使用方法请看:https://xueyuanjun.com/post/8179 ```php engine = 'InnoDB';//指定表的存储引擎(MySQL) //$table->charset = 'utf8';//指定数据表的默认字符集(MySQL) //$table->collation = 'utf8_unicode_ci';//指定数据表的字符序(MySQL) $table->increments('id');//自增字段 $table->integer('uid') ->comment('用户唯一ID'); $table->string('email') ->comment('用户注册邮箱') ->nullable();//表示该字段允许为空 $table->string('nickname', 24)//第二个参数可以限定长度 ->comment('用户昵称'); $table->string('password', 32) ->comment('用户密码'); $table->timestamp('reg_time') ->comment('用户注册时间'); }); } /** * Reverse the migrations. * * @return void */ public function down() { // } } ``` # 创建表 ```shell php artisan migrate ``` --- 常见错误:https://nowtime.cc/laravel/1155.html
Laravel 资源控制器操作处理 作者: Chuwen 时间: 2020-11-18 分类: Laravel,PHP 评论 # 命令行快捷生成控制器文件 > 这个命令将会生成一个控制器 app/Http/Controllers/PhotoController.php 。其中包括每个可用资源操作的方法。 ```shell php artisan make:controller PhotosController --resource ``` > 接下来,你可以给控制器注册一个资源路由: ```shell Route::resource('photos', 'PhotoController'); ``` HTTP 方法 URI 动作 路由名称 GET /photos index photos.index GET /photos/create create photos.create POST /photos store photos.store GET /photos/{photo} show photos.show GET /photos/{photo}/edit edit photos.edit PUT/PATCH /photos/{photo} update photos.update DELETE /photos/{photo} destroy photos.destroy