現(xiàn)在是2017年2月5日项戴,從開始接觸Laravel也有小一年來了三痰,這篇文章將記錄我使用Laravel的一些新得腰懂。新的想法也將隨時更新到這篇文章中玻粪。
1.目錄別名
1.1說明
在ShenCom他們有一點(diǎn)做的很好隅津,就是把框架外的擴(kuò)展全部放在了一個獨(dú)立的目錄里面,沒有去破壞Laravel框架本身的結(jié)構(gòu)劲室,讓框架保持了原來的特性伦仍,又增加了新的功能。
1.2用法
1.2.1添加主服務(wù)文件
在 app\Providers\AppServiceProvider的boot方法中添加
App::register("\YaZhou\YaZhouServiceProvider");
1.2.2起別名
在compser.json里面增加一個autoload選項很洋。
執(zhí)行命令composer dump-autpload
2.模型(Model)的定義
2.1簡單示例
namespace Yz\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Test extends Model{
use SoftDeletes;
protected $primaryKey = "id";
protected $table = "test";
protected $guarded = ["id"];
public $timestamps = false;
}
2.2代碼說明
protected $table
指定當(dāng)前模型對應(yīng)的數(shù)據(jù)表名充蓝。
protected $primaryKey
指定當(dāng)前數(shù)據(jù)表的主鍵,默認(rèn)id蹲缠。
public $timestamps
默認(rèn)情況下棺克,Eloquent期望created_at和updated_at已經(jīng)存在于數(shù)據(jù)表中,如果你不想要這些Laravel自動管理的列线定,在模型類中設(shè)置$timestamps屬性為false。
protected $guarded/$fillale
在使用create方法保存一個新的模型時确买,所有的Eloquent模型都通過批量賦值(Mass Assignment)進(jìn)行保護(hù)斤讥,需要指定模型的fillable或guarded屬性。在fillable里面的都是可以被賦值的(白名單)湾趾,在guarded里面的都是不可以被賦值的(黑名單)芭商,fillable和guarded二選一。
use SoftDeletes
軟刪除搀缠,要求數(shù)據(jù)表中以一個字段“deleted_at”铛楣。被軟刪除的記錄并沒有被真正的刪除,只是在“deleted_at”字段記錄了操作時間艺普。
Model::withTrashed()->...
簸州,同時獲取被軟刪除的記錄鉴竭。
Model::onlyTrashed()->...
,只獲取被軟刪除的記錄岸浑。
$record->restore();
搏存,恢復(fù)被軟刪除的記錄。
$record->forceDelete()
矢洲,強(qiáng)制刪除璧眠。
$record-> history() -> forceDelete()
,強(qiáng)制刪除所有關(guān)模型读虏。
2.3擴(kuò)展
查詢作用域
作用域允許你定義一個查詢條件的通用集合责静,這樣就可以在應(yīng)用中方便地復(fù)用。詳情參見盖桥,Laravel5.1中文文檔泰演。
3.模型關(guān)系
3.1一對一關(guān)系
3.1.1關(guān)系的定義
namespace Yz\Test\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model{
// 獲取關(guān)聯(lián)到用戶的手機(jī)
public function phone(){
return $this->hasOne("Yz\Test\Models\Phone");
// 這種寫法默認(rèn)Phone模型中有一個user_id外鍵,默認(rèn)與當(dāng)前模型的id關(guān)聯(lián)
// return $this->hasOne("Yz\Test\Models\Phone", "user_id");
// 指定Phone模型中的外鍵葱轩,默認(rèn)與當(dāng)前模型的id關(guān)聯(lián)
// return $this->hasOne("Yz\Test\Models\Phone", "user_id", "id");
// 指定phone模型的外鍵睦焕,同時指定當(dāng)前模型的關(guān)聯(lián)鍵,推薦使用
}
}
3.1.2定義關(guān)系的相對關(guān)聯(lián)
namespace Yz\Test\Models;
use Illuminate\Database\Eloquent\Model;
class Phone extends Model{
public function user(){
// 找到電話所屬的用戶
return $this->belongsTo("Yz\Test\Models\User");
// 默認(rèn)通過當(dāng)前表的user_id去匹配user表的id
// return $this->belongsTo("Yz\Test\Models\User", "user_id");
// 指定當(dāng)前表的關(guān)聯(lián)字段靴拱,去與主表的id字段進(jìn)行匹配
// return $this->belongsTo("Yz\Test\Models\User", "user_id", "id");
// 指定當(dāng)前表的user_id與User表的id進(jìn)行匹配垃喊,推薦使用
}
}
3.1.3一對一關(guān)系的使用
$phone = User::find(1)->phone;
3.2 一對多關(guān)系
3.2.1關(guān)系的定義
namespace Yz\Test\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends Model{
/*博客數(shù)據(jù)*/
public function comments(){
// 獲取博客的評論
return $this->hasMany("Yz\Test\Models\Comment");
// $this->hasMany("Yz\Test\Models\Comment", "post_id");
// $this->hasMany("Yz\Test\Models\Comment", "post_id", "id");
}
}
3.2.2定義關(guān)系的相對關(guān)聯(lián)
namespace Yz\Test\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model{
/*博客的評論*/
public function post(){
return $this->belongsTo("Yz\Test\Models\Post");
// return $this->belongsTo("Yz\Test\Models\Post", "post_id");
// return $this->belongsTo("Yz\Test\Models\Post", "post_id", "id");
}
}
3.2.3一對多關(guān)系的使用
$comments = App\Post::find(1)->comments;
$comments = App\Post::find(1)->comments()->where('title', 'foo')->first();
$comment = App\Comment::find(1);
echo $comment->post->title;
3.3多對多關(guān)系
實現(xiàn)多對多關(guān)系需要一個中間表。比如,一個用戶(user)擁有多種角色(role)牲剃,那么我們就需要一個中間表(user_role)蠢莺。
3.3.1關(guān)系的定義
public function roles(){
return $this->belongsToMany("Yz\Test\Model\Role");
// 省略寫法,要求中間表的名字為role_user乌助,且中間表包含role_id和user_id兩個字段
// return $this->belongsToMany("Yz\Test\Models\Role", "user_role");
// 半完整寫法
// return $this->belongsToMany("Yz\Test\Models\Role", "user_role", "user_id", "role_id");
// 完整寫法
}
3.3.2定義關(guān)系的相對關(guān)聯(lián)
namespace Yz\Test\Models;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
// 用戶角色
public function users()
{
// 找到屬于當(dāng)前角色的用戶
return $this->belongsToMany("Yz\Test\Models\User");
// return $this->belongsToMany("Yz\Test\Models\User", "user_role");
// return $this->belongsToMany("Yz\Test\Models\User", "user_role", "role_id", "user_id");
}
}
3.3.3多對多關(guān)系的使用
$user = App\User::find(1);
foreach ($user->roles as $role) {
//
}
$roles = App\User::find(1)->roles()->orderBy('name')->get();
3.4插入關(guān)聯(lián)模型
3.4.1插入一個關(guān)聯(lián)模型
$comment = new App\Comment(['message' => 'A new comment.']);
$post = App\Post::find(1);
$comment = $post->comments()->save($comment);
3.4.2插入多個關(guān)聯(lián)模型
$post = App\Post::find(1);
$post->comments()->saveMany([
new App\Comment(['message' => 'A new comment.']),
new App\Comment(['message' => 'Another comment.']),
]);
除了save和saveMany方法外,還可以使用create方法陌知,該方法接收屬性數(shù)組他托、創(chuàng)建模型、然后插入數(shù)據(jù)庫仆葡。save和create的不同之處在于save接收整個Eloquent模型實例而create接收原生PHP數(shù)組赏参。
$post = App\Post::find(1);
$comment = $post->comments()->create([
'message' => 'A new comment.',
]);
3.4.3更新關(guān)聯(lián)模型
更新belongsTo關(guān)聯(lián)的時候,可以使用associate方法沿盅,該方法會在子模型設(shè)置外鍵把篓。
$account = App\Account::find(10);
$user->account()->associate($account);
$user->save();
3.4.4移除關(guān)聯(lián)模型
移除belongsTo關(guān)聯(lián)的時候,可以使用dissociate方法腰涧。該方法在子模型上取消外鍵和關(guān)聯(lián)韧掩。
$user->account()->dissociate();
$user->save();
3.4.5多對多關(guān)系的附加
$user = App\User::find(1);
$user->roles()->attach($roleId);
附加關(guān)聯(lián)關(guān)系到模型,還可以以數(shù)組形式傳遞額外被插入數(shù)據(jù)到中間表:
$user->roles()->attach($roleId, ['expires' => $expires])
3.4.6多對多關(guān)系的移除
// 從指定用戶中移除角色...
$user->roles()->detach($roleId);
// 從指定用戶移除所有角色...
$user->roles()->detach();
為了方便窖铡,attach和detach還接收數(shù)組形式的ID作為輸入疗锐。
$user = App\User::find(1);
$user->roles()->detach([1, 2, 3]);
$user->roles()->attach([1 => ['expires' => $expires], 2, 3]);
3.4.7觸發(fā)父級時間戳
當(dāng)一個模型屬于另外一個時坊谁,例如Comment屬于Post,子模型更新時父模型的時間戳也被更新將很有用窒悔,例如呜袁,當(dāng)Comment模型被更新時,你可能想要”觸發(fā)“創(chuàng)建其所屬模型Post的updated_at時間戳简珠。Eloquent使得這項操作變得簡單阶界,只需要添加包含關(guān)聯(lián)關(guān)系名稱的touches屬性到子模型中即可。