users'); 2.6 运行一个通用语句 有些数据库语句不返回任何值,对于这种类型的操作,可以使用 DB 门面的 statement 方法: DB::statement('drop
table users'); 2.7 监听查询事件 如果你想要获取应用中每次 SQL 语句的执行,可以使用 listen 方法,该方法对查询日志和 调试非常有用,你可以在服务提供者中注册查询监听器: 用 transaction 方法时不需要担心手动回滚或提交: DB::transaction(function () { DB::
table('users')->update(['votes' => 1]); DB::
table('posts')->delete(); }); 3.1 手动使用事务 如果你想要手动开始事务从而对回滚和提交有一个完整的控制,可以使用 注意:Laravel 查询构建器使用 PDO 参数绑定来避免 SQL 注入攻击,不再需要清除传递到 绑定的字符串。 2、获取结果集 2.1 从一张表中取出所有行 在查询之前,使用 DB 门面的
table 方法,
table 方法为给定表返回一个查询构建器,允许 你在查询上链接更多约束条件并最终返回查询结果。在本例中,我们使用 get 方法获取表中 所有记录:
0 码力 |
307 页 |
3.46 MB
| 1 年前 3
如果你不想要迁移查询构建器结果到 Collection 实例,可以在查询构建器的 get 方法后调用 call 方法,这将会返回原生的 PHP 数组结果,从而保证向后兼容: $users = DB::table('users')->get()->all(); Eloquent $morphClass 属性 可以在 Eloquent 模型上定义的$morphClass 属性已经被移除,以便定义一个“morph $query->join('table', function($join) { $join->on('foo', 'bar')->where('bar', 'baz'); }); $bindings 属性也被移除,要直接操作 join 绑定可以使用 addBinding 方法: $query->join(DB::raw('('.$subquery->toSql().') table'), function($join) Schema::create('sessions', function ($table) { $table->string('id')->unique(); $table->integer('user_id')->nullable(); $table->string('ip_address', 45)->nullable(); $table->text('user_agent')->nullable();
0 码力 |
691 页 |
9.37 MB
| 1 年前 3
Schema::create('sessions', function ($table) { $table->string('id')->unique(); $table->unsignedInteger('user_id')->nullable(); $table->string('ip_address', 45)->nullable(); $table->text('user_agent')->nullable(); ->nullable(); $table->text('payload'); $table->integer('last_activity'); }); 你可以使用 Artisan 命令 session:table 在数据库中创建这张表: php artisan session:table php artisan migrate Redis 在 Laravel 109 distinct 处理数组时,验证字段不能包含重复值: 'foo.*.id' => 'distinct' email 验证字段必须是格式正确的电子邮件地址 exists:table,column 验证字段必须存在于指定数据表 基本使用: 'state' => 'exists:states' 指定自定义列名: 'state' => 'exists:states
0 码力 |
377 页 |
14.56 MB
| 1 年前 3
授权资源 & viewAny 字符串 & 数组辅助函数 影响中等 认证 RegisterController 不再支持 Carbon 1.x 数据库 Capsule::table 方法 Eloquent 数组化 & toArray Eloquent BelongsTo::update 方法 Eloquent 主键类型 本地化 Lang::trans 需要在 .env 文件中更新这个环境变量的名称。 数据库 Capsule table 方法 影响级别:中等 注:此更新只会影响使用了 illuminate/database 依赖 包的非 Laravel 应用。 Illuminate\Database\Capsule\Manager 类的 table 方法签名被 更新为接收数据表别名作为第二个参数,如果你在 Laravel 应用之 fluent query builder instance. * * @param \Closure|\Illuminate\Database\Query\Builde r|string $table * @param string|null $as * @param string|null $connection * @return \Illuminate\Database\Query\Builder
0 码力 |
1442 页 |
14.66 MB
| 1 年前 3
1 Table of Contents Preface The Laravel Handbook Conclusion 2 Preface The Laravel Handbook follows the 80/20 rule: learn in 20% of the time the 80% of a topic. In particular, the goal is to get bootcamp.dev. You can reach me on Twitter @flaviocopes. Enjoy! 3 The Laravel Handbook 0. Table of contents 0. Table of contents 1. Introduction to Laravel 2. Getting started 3. Blade 4. Dynamic routes will use to create the database table(s) we need to use: php artisan make:migration initial_table_creation 23 This command created the 2023_05_11_080724_initial_table_creation.php file (the date
0 码力 |
111 页 |
14.25 MB
| 1 年前 3