數據庫操作之Eloquent ORM(一)

模型的名字加上s就是表的名字

例如 創建 model user.php 表的名字是users 

如果表的名字不是users 

protected $table = 'user';

//指定主鍵

 protected $primaryKey = 'id';

注意:在控制器當中使用的時候 use App\User;

all

$user = user::all();
dd($user);//返回一個集合

find 一條數據

$user = user::find(1);
dd($user);//返回一個集合

findOrFail()根據主鍵查找沒有異常     

$user = user::findOrFail(1);

使用查詢構造器

$user = user::get();
//之前使用 
$user = DB::table('web_user')->get();

聚合函數

DB::table('web_user')->count();
$user = user::count();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章