環境判斷:
$environment = app()->environment();
if (app()->environment('local')){}
if (app()->environment(['local', 'staging'])){}
外來鍵約束
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'|'restrict'|'set null'|'no action');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade'|'restrict'|'set null'|'no action');
$table->dropForeign(array('user_id'));
$table->dropForeign('posts_user_id_foreign');
雜湊
Hash::make('secretpassword');
Hash::check('secretpassword', $hashedPassword);
Hash::needsRehash($hashedPassword);
加密解密
Crypt::encrypt('secretstring');
Crypt::decrypt($encryptedString);
Crypt::setMode('ctr');
Crypt::setCipher($cipher);
Config
Config::get('app.timezone')
Config::get('app.timezone', 'default');
Config::set('database.default', 'sqlite');
config()->get('app.timezone');
config('app.timezone', 'default');
config(['database.default' => 'sqlite']);
路由約束
Route::get('foo/{bar}', function($bar){})
->where('bar', '[0-9]+');
Route::get('foo/{bar}/{baz}', function($bar, $baz){})
->where(array('bar' => '[0-9]+', 'baz' => '[A-Za-z]'))
Route::pattern('bar', '[0-9]+')
動態where
$admin = DB::table('users')->whereId(1)->first();
$john = DB::table('users')
->whereIdAndEmail(2, 'john@doe.com')
->first();
$jane = DB::table('users')
->whereNameOrAge('Jane', 22)
->first();
$users = DB::table('users')
->orderBy('name', 'desc')
->groupBy('count')
->having('count', '>', 100)
->get();
快取
Cache
cache()
Cache::put('key', 'value', $seconds);
Cache::put('key', 'value');
Cache::add('key', 'value', $seconds);
Cache::forever('key', 'value');
Cache::sear('key', function(){ return 'value' });
Cache::remember('key', $seconds, function(){ return 'value' });
Cache::rememberForever('key', function(){ return 'value' });
Cache::forget('key');
Cache::has('key');
Cache::get('key');
Cache::get('key', 'default');
Cache::get('key', function(){ return 'default'; });
Cache::pull('key');
Cache::flush();
Cache::increment('key');
Cache::increment('key', $amount);
Cache::decrement('key');
Cache::decrement('key', $amount);
Cache::tags('my-tag')->put('key','value', $seconds);
Cache::tags('my-tag')->has('key');
Cache::tags('my-tag')->get('key');
Cache::tags(['people', 'artists'])->put('John', $john, $seconds);
Cache::tags('my-tag')->forget('key');
Cache::tags('my-tag')->flush();
Cache::tags(['people', 'authors'])->flush();
Cache::section('group')->put('key', $value);
Cache::section('group')->get('key');
Cache::section('group')->flush();
Cache::tags(['people', 'artists'])->put('John', $john, $seconds);
cache('key');
cache(['key' => 'value'], $seconds);
cache(['key' => 'value'], now()->addMinutes(10));
cache()->remember('users', $seconds, function() { return User::all(); });
Cache::store('file')->get('foo');
Cache::store('redis')->put('name', 'Jack', 600);
模型
模型關聯
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
return $this->hasMany('App\Comment', 'foreign_key', 'local_key');
return $this->belongsTo('App\Post', 'foreign_key', 'other_key');
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
return $this->belongsToMany('App\User');
$role->pivot->created_at;
return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');
return $this->belongsToMany('App\Role')->withTimestamps();
return $this->hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
return $this->morphTo();
return $this->morphMany('App\Photo', 'imageable');
return $this->morphMany('App\Photo', 'imageable');
Relation::morphMap([
'Post' => App\Post::class,
'Comment' => App\Comment::class,
]);
return $this->morphToMany('App\Tag', 'taggable');
return $this->morphToMany('App\Tag', 'taggable');
return $this->morphedByMany('App\Post', 'taggable');
return $this->morphedByMany('App\Video', 'taggable');
$user->posts()->where('active', 1)->get();
$posts = App\Post::has('comments')->get();
$posts = Post::has('comments', '>=', 3)->get();
$posts = Post::has('comments.votes')->get();
$posts = Post::whereHas('comments', function ($query) {
$query->where('content', 'like', 'foo%');
})->get();
$books = App\Book::with('author')->get();
$books = App\Book::with('author', 'publisher')->get();
$books = App\Book::with('author.contacts')->get();
$books->load('author', 'publisher');
$comment = new App\Comment(['message' => 'A new comment.']);
$post->comments()->save($comment);
$post->comments()->saveMany([
new App\Comment(['message' => 'A new comment.']),
new App\Comment(['message' => 'Another comment.']),
]);
$post->comments()->create(['message' => 'A new comment.']);
$user->account()->associate($account);
$user->save();
$user->account()->dissociate();
$user->save();
$user->roles()->attach($roleId);
$user->roles()->attach($roleId, ['expires' => $expires]);
$user->roles()->detach($roleId);
$user->roles()->detach();
$user->roles()->detach([1, 2, 3]);
$user->roles()->attach([1 => ['expires' => $expires], 2, 3]);
$user->roles()->sync([1, 2, 3]);
$user->roles()->sync([1 => ['expires' => true], 2, 3]);
本作品採用《CC 協議》,轉載必須註明作者和本文連結