php artisan migrate //執行數據庫的遷移操做
提供了php cli 方式進行建立model類,負責與數據庫交互php
php artisan make:model Page
php artisan make:controller Admin/AdminHomeController
ORM:即'Object Relational Mapping' 對象關係映射,ORM出現是爲了幫咱們把對數據庫的操做變得更加方便laravel
要在數據庫交易運行一組操做,你能夠在DB facade 使用transaction方法. 若是閉包運行成功,交易將會自動提交數據庫
DB::transaction(function(){ DB::table('users') -> update(['votes' => 1]) DB::table('posts') -> delete() });
若是你想手動處理交易而且徹底控制還原或提交,你能夠在DB facade 使用 beginTransaction
數組
DB::beginTransaction(); //還原 DB::rollBack(); //提交 DB::commit();
獲取pdo實例$pdo = DB::connection()->getPdo();
閉包
默認狀況下,Eloquent 預期你的數據表會有 created_at
和 updated_at
字段若是你不但願讓 Eloquent 來自動維護這兩個字段,在你的模型內將 $timestamps
屬性設置爲 false
app
若是須要自定義你的時間戳格式,在你的模型內設置$dateFormat
屬性,這個屬性決定了日期在數據庫中如何保存函數
DB::table('表名')->get() 獲取數據庫全部字段的信息
從數據表中取得單一列或者欄,若你只需從數據表中取出單一列,你能夠使用 first
方法。這個方法會返回單一的 StdClass
對象:post
DB::table('grades')->where('username','jack')->first()
從數據表中將結果切塊:使用chunk
方法,這個方法一次取出一小塊結果,並將每一個區塊餵給一個閉包處理ui
DB::table('grades')->chunk(100, function($users) { foreach ($users as $user) { echo $user->username //not $user['username']; } });
能夠對閉包的結果返回false
已中止對後續的操做rest
聚合:查詢構建器提供了多種聚合方法:count
,max
,min
,avg
,sum
能夠在查找後調用任何一個方法
$data = DB::table('grades')->count(); $data = DB::table('grades')->max(); $data = DB::table('grades')->min();
能夠使用select
方法爲查找一個自定義的select
子句
$user = DB::table('grades')->select('username','username as qq')->get();
distinct
容許你強制讓查找傳回不重複的結果
$data = DB::table('grades')->distinct()->get()
若已經有一個查詢構建器的實例,但願在既存的select
子句加入一個字段,你能夠使用addSelect
方法
$query = DB::table('grades')->select('username') $users = $query->addSelect('sex')->get();
Inner join 語法 查詢構建器也能夠編寫join語法,傳入join
的方法的第一個參數是你須要鏈接的數據表的名稱,其餘參數則用以鏈接的字段約束,能夠在單一查找鏈接多個數據表
$user = DB::table('grades') ->join('表名','grades.id','=','表名.id') ->join('表名1','grades.sex','=','表名1.sex') ->get() 左鏈接,右鏈接同理
where 子句:
經過where 來增長查詢條件
$users = DB:table('grades')->where('username','=','jack')->get() 也能夠使用其餘運算符號 $users = DB:table('grades')->where('id','>=','12')->get() //不等於 $users = DB:table('grades')->where('username','<>','jack')->get() //like $users = DB:table('grades')->where('username','like','q%')->get()
//使用orwhere
$users = DB::table('grades')>where('id','>','24')>orWhere('username','gewenrui2')->get();
其餘的where子句
高端where子句:
$users = DB::table('grades')->where('username','=','gewenrui2') ->orWhere(function($query){$query ->where('id','>=','22') ->where('sex','=','nan'); })->get(); 至關於 //select * from grades where username = 'gewenrui2' or(id >=22 and sex = nan)
whereExits 方法容許你編寫SQL子句,whereExits接受一個閉包參數
DB::table('users') ->whereExists(function ($query) { $query->select(DB::raw(1)) ->from('orders') ->whereRaw('orders.user_id = users.id'); }) ->get(); //至關於 select * from users where exits(select 1 from orders.user_id = users_id)
爲查找的結果分組
$users = DB::table('grades') ->groupBy('username') ->having('id','>=',22)->get(); //select * from grades group by username having id >=22
skip 與 take
要限制查找所返回的結果數量,或略過給定數量的查找結果(偏移),你可以使用 skip 和 take 方法:
$users = DB::table('users')->skip(10)->take(5)->get();
Insert
insert接受一個數組,包含要插入的字段名稱以及值
$users = DB::table('grades')->insert( ['username' => 'wenruige', 'stu_id' => 1330090604, 'sex' => 'male', 'grade' => 100 ] ); 若數據表有一自動遞增的 id,使用 insertGetId 方法來插入記錄並取得其 ID: $users = DB::table('grades')->insertGetId( ['username' => 'wenruige', 'stu_id' => 1330090604, 'sex' => 'male', 'grade' => 100 ] );
Updates:可以使用 update 方法讓查找產生器更新已存在的記錄。update 方法和 insert 方法同樣,接受含一對字段及值的數組,其中包含要被更新的字段。你能夠使用 where 子句來約束 update 查找
DB::table('grades')->where('sex','male')->update(['username' => 'qq']);
Delete:將記錄從表中刪除
//刪除一個文件 DB::table('grades')->delete() //經過限制條件來定向刪除記錄 DB::table('grades')->where('id','=',21)->delete(); //若你但願截去整個數據表來移除全部數據列,並將自動遞增 ID 重設爲零,你能夠使用 truncate 方法 DB::table('grades')->truncate();
悲觀鎖定 產詢產生器也包含一些函數,用以協助你在 select 語法上做「悲觀鎖定」。要以「共享鎖」來運行述句,你能夠在查找上使用 sharedLock 方法。共享鎖可避免選擇的數據列被更改,直到你的交易提交爲止:
DB::table('users')->where('votes', '>', 100)->sharedLock()->get();
此外,你能夠使用 lockForUpdate 方法。「用以更新」鎖可避免數據列被其餘共享鎖修改或選取:
DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
找不到的例外:
findOrFail
以及firstOrFail
方法會返回查找的第一個結果,將會拋出一個異常
若是沒有捕捉到意外,會自動返回HTTP404
迴應給用戶,因此當使用這些方法時,沒有必要明確的編寫404
迴應
添加和更新模型
要在數據庫中建立一條新記錄,只要建立一個新模型實例,並在模型上設置屬性,再調用 save
方法
$flight = new Flight; $flight->name = $request->name; $flight->save(); 當 save 方法被調用時,created_at 以及 updated_at 時間戳記將會自動被設置,因此不須要手動去設置它們。
save 方法也能夠用於更新數據庫中已經存在的模型。要更新模型,你必須先取回模型,設置任何你但願更新的屬性,接着調用 save 方法。一樣的,updated_at 時間戳記將會自動被更新,因此不須要手動設置它的值
$flight = App\Flight::find(1); $flight->name = 'New Flight Name'; $flight->save();
批量賦值
須要在你的模型上指定一個 fillable 或 guarded 屬性,由於全部的 Eloquent 模型有針對批量賦值(Mass-Assignment)作保護。
protected $fillable = ['name'];
$fillable
做爲一個可批量賦值屬性的白名單,$guarded
更像是黑名單
刪除模型
刪除模型在實例上調用delete
方法
$data = App\Data::find(1); $data->delete();
透過鍵來刪除現有的模型:
咱們在調用delete模型,先從數據庫取回了模型,若是知道了主鍵就能夠不取回模型直接刪除它
App\Data::destroy(1) App\Data::destroy([1,2,3])
經過查找來刪除模型
App\Data::where('name',1)->delete();
軟刪除
除了實際從數據庫中移除記錄,Eloquent 也能夠「軟刪除」模型。當模型被軟刪除時,它們不是真的從數據庫中被移除。而是 deleted_at 屬性被設置在模型上並添加到數據庫。若是模型有一個非空值的 deleted_at,表明模型已經被軟刪除了。要在模型啓動軟刪除,必須在模型上使用
use SoftDeletes; protected $dates = ['deleted_at'];
被軟刪除的模型將會自動從全部的查找結果中排除。然而,你能夠藉由在查找上調用 withTrashed 方法, 強迫被軟刪除的模型出如今查找結果
$flights = App\Data::withTrashed() ->where('id', 1) ->get();
時候你可能但願「取消刪除」一個被軟刪除的模型。要恢復一個被軟刪除的模型回到有效狀態,必須在模型實例上使用 restore 方法
$flight->restore();
當一個新模型初次被保存,將會觸發 creating 以及 created 事件。若是一個模型已經存在於數據庫並且調用了 save 方法,將會觸發 updating 和 updated 事件。然而,在這兩個情況下,都將會觸發 saving 和 saved 事件。
public function boot() { User::creating(function ($user) { if ( ! $user->isValid()) { return false; } }); }
咱們來在服務提供者中定義一個 Eloquent 事件監聽器。在咱們的事件監聽器中,咱們會在給定的模型上調用 isValid 方法,並在模型無效的時候返回 false。從 Eloquent 事件監聽器返回 false 會取消 save 和 update 的操做