Laravel 8
Laravel 8 备忘清单是最好的 Laravel 代码段和备忘清单参考
入门
Artisan
BASH
1 | # 5.1.11 新增:http://laravel.com/docs/authorization#creating-policies |
Composer
BASH
1 | $ composer create-project laravel/laravel folder_name |
Config
PHP
1 | Config::get('app.timezone'); |
Environment
PHP
1 | $environment = app()->environment(); |
Log 日志
PHP
1 | // 记录器提供 RFC 5424 中定义的七个日志记录级别: |
查询记录
PHP
1 | // 启用日志 |
URL
PHP
1 | URL::full(); |
Event
PHP
1 | Event::fire('foo.bar', array($bar)); |
Pagination
PHP
1 | // 自动魔术分页 |
Lang
PHP
1 | App::setLocale('en'); |
File
PHP
1 | File::exists('path'); |
SSH
执行命令
PHP
1 | SSH::run(array $commands); |
任务
PHP
1 | // 定义 |
SFTP 上传
PHP
1 | SSH::put($localFile, $remotePath); |
Cookie
PHP
1 | Cookie::get('key'); |
UnitTest
安装并运行
PHP
1 | // 添加到作曲家并更新: |
断言
PHP
1 | $this->assertTrue(true); |
调用路由
PHP
1 | $response = $this->call($method, $uri, $parameters, $files, $server, $content); |
Cache
PHP
1 | Cache::put('key', 'value', $minutes); |
Session
PHP
1 | Session::get('key'); |
Response
PHP
1 | return Response::make($contents); |
Request
PHP
1 | // url: http://xx.com/aa/bb |
Container
PHP
1 | App::bind('foo', function($app){ return new Foo; }); |
Redirect
PHP
1 | return Redirect::to('foo/bar'); |
Security
Hashing
PHP
1 | Hash::make('secretpassword'); |
Encryption
PHP
1 | Crypt::encrypt('secretstring'); |
Queue
PHP
1 | Queue::push('SendMail', array('message' => $message)); |
Starting the queue listener
BASH
1 | php artisan queue:listen |
View
PHP
1 | View::make('path/to/view'); |
Validation
PHP
1 | Validator::make( |
Rules
- accepted
- active_url
- after:YYYY-MM-DD
- before:YYYY-MM-DD
- alpha
- alpha_dash
- alpha_num
- array
- between:1,10
- confirmed
- date
- date_format:YYYY-MM-DD
- different:fieldname
- digits:value
- digits_between:min,max
- boolean
- exists:table,column
- image
- in:foo,bar,...
- not_in:foo,bar,...
- integer
- numeric
- ip
- max:value
- min:value
- mimes:jpeg,png
- regex:[0-9]
- required
- required_if:field,value
- required_with:foo,bar,...
- required_with_all:foo,bar,...
- required_without:foo,bar,...
- required_without_all:foo,bar,...
- same:field
- size:value
- timezone
- unique:table,column,except,idColumn
- url
Form
PHP
1 | Form::open(array('url' => 'foo/bar', 'method' => 'PUT')); |
Form Elements
PHP
1 | Form::label('id', 'Description'); |
String
PHP
1 | // 将 UTF-8 值音译为 ASCII |
Blade
PHP
1 | // 在模板中显示一个部分 |
HTML
PHP
1 | HTML::macro('name', function(){}); |
DB
基本数据库使用
PHP
1 | DB::connection('connection_name'); |
查询生成器
PHP
1 | // 从表中检索所有行 |
Joins
基本加入声明
PHP
1 | DB::table('users') |
左连接语句
PHP
1 | DB::table('users') |
Aggregates
PHP
1 | $users = DB::table('users')->count(); |
原始表达式
PHP
1 | $users = DB::table('users') |
Inserts 插入
PHP
1 | DB::table('users')->insert( |
Updates 更新
PHP
1 | DB::table('users') |
Deletes 删除
PHP
1 | DB::table('users')->where('votes', '<', 100)->delete(); |
Unions 联合
PHP
1 | // unionAll() 方法也可用,并且具有与 union 相同的方法签名 |
Input
Input
PHP
1 | Input::get('key'); |
Session Input (flash)
PHP
1 | // 会话的闪存输入 |
Files
PHP
1 | // 使用已上传的文件 |
路由
Route
PHP
1 | Route::get('foo', function(){}); |
RESTful 控制器
PHP
1 | Route::resource('posts','PostsController'); |
触发错误
PHP
1 | App::abort(404); |
路由参数
PHP
1 | Route::get('foo/{bar}', function($bar){}); |
HTTP 动词
PHP
1 | Route::any('foo', function(){}); |
Secure Routes(TBD)
PHP
1 | Route::get('foo', array('https', function(){})); |
路由约束
PHP
1 | Route::get('foo/{bar}', function($bar){}) |
HTTP 中间件
PHP
1 | // 为路由分配中间件 |
命名路由
PHP
1 | Route::currentRouteName(); |
路由前缀
PHP
1 | Route::group(['prefix' => 'admin'], function() |
路由命名空间
PHP
1 | // 该路由组将携带命名空间“Foo\Bar” |
子域路由
PHP
1 | // {sub} 将被传递给闭包 |
Model
基本用法
PHP
1 | // 定义 Eloquent 模型 |
More
PHP
1 | Model::create(array('key' => 'value')); |
Soft Delete
PHP
1 | Model::withTrashed()->where('cars', 2)->get(); |
Events
PHP
1 | Model::creating(function($model){}); |
Eloquent Configuration
PHP
1 | // 禁止从模型插入和更新中抛出批量分配异常 |
Schema
Schema
PHP
1 | // 表示需要创建表 |
Indexes
PHP
1 | $table->string('column')->unique(); |
Foreign Keys
PHP
1 | $table->foreign('user_id')->references('id')->on('users'); |
列类型 - 增量
PHP
1 | $table->increments('id'); |
列类型 - 字符串和文本
PHP
1 | $table->char('name', 4); |
列类型 - 数字
PHP
1 | $table->integer('votes'); |
列类型 - 日期和时间
PHP
1 | $table->date('created_at'); |
列类型 - 添加 created_at 和 updated_at 列
PHP
1 | $table->timestamps(); |
列类型 - 其他
PHP
1 | $table->binary('data'); |
PHP
1 | Mail::send('email.view', $data, function($message){}); |
Messages
PHP
1 | // 这些可以用在传递给 Mail::send() 或 Mail::queue() 的 $message 实例上 |
Auth
Authentication
PHP
1 | // 判断当前用户是否通过认证 |
Authorization
PHP
1 | // 定义能力 |
Helper
Arrays
PHP
1 | // 将给定的键/值对添加到数组中,如果 |
Paths
PHP
1 | // 应用程序目录的完全限定路径 |
Miscellaneous
PHP
1 | // 验证器实例(Auth) |
Strings
PHP
1 | // 将值转换为驼峰大小写 |
URLs and Links
PHP
1 | action('FooController@method', $parameters); |