laravel框架創建:TrGoModelphp
<?php namespace TrChaos\Model; class TrGoModel extends Model { protected $table = 'trgo_chip'; protected $fillable = [ 'id','item_id','sku_id','item_num','chip_code','created_at','updated_at','deleted_at' ]; } ?>
<?php $users = DB::table('user')->get(); foreach($users as $user){ var_dump($user->name); } $users->toArray(); // 轉換成數組 ?>
<?php DB::table('users')->where('name', 'John')->first();
<?php $name = DB::table('users')->where('name','John')->pluck('name'); 換作Laravel中的寫法,先定義好Model // 芯片記錄 $chip_codes = TrGoModel::query()->where('deleted_at', '=', null)->pluck('chip_code');
<?php $roles = DB::table('roles')->lists('title'); // 從新命名title值爲name $roles = DB::table('roles')->lists('title','name');
<?php $users = DB::table('users')->select('name','email')->get(); $users = DB::table('users')->distinct()->get(); $users = DB::table('users')->select('name as user_name')->get(); // 增長查詢子句到現有的查詢中 $users = DB::table('users')->select('name')->addSelect('age')->get();
<?php $users = DB::table('users')->where('votes', '>', 100)->orWhere('name','John')->get(); $users = DB::table('users')->whereBetween('votes',[1,100])->get(); $user = DB::table('users')->whereNotBetween('votes',[1,100])->get(); $users = DB::table('users')->whereIn('id',[1,2,3])->get(); $users = DB::table('users')->whereNotIn('id',[1,3])->get(); $users = DB::table('users')->whereNull('updated_at')->get(); $admin = DB::table('users')->whereId(1)->first(); $john = DB::table('users')->whereIdAndEmail(2, 'john@ss.com')->first(); $jane = DB::table('users')->whereNameOrAge('jane',22)->first(); ?>
<?php $users = DB::table('users')->orderBy('name','desc')->groupBy('count')->having('count','>',100)->get(); // 偏移(offset)及限制Limit $users = DB::table('users')->skip(10)->table(5)->get();
<?php DB::table('users')->join('contacts','users.id','=','contacts.user_id') ->join('orders','users.id','=','orders.user_id') ->select('user.id','contacts.phone','orders.price') ->get(); // LEFT JOIN DB::table('users')->LEFTJoin('posts','users.id','=','posts.user_id')->get(); DB::table('users')->join('contacts',function($join){ $join->on('users.id','=','contacts.user_id')->orOn(...); })->get(); DB::table('users') ->join('contacts', function($join) { $join->on('users.id', '=', 'contacts.user_id') ->where('contacts.user_id', '>', 5); }) ->get();
<?php //有些時候你須要更高級的 where 子句,如「where exists」或嵌套的羣組化參數。Laravel //的查詢構造器也能夠處理這樣的狀況: DB::table('users') ->where('name', '=', 'John') ->orWhere(function($query) { $query->where('votes', '>', 100) ->where('title', '<>', 'Admin'); }) ->get(); /* 上面的查找語法會產生下方的 SQL: select * from users where name = 'John' or (votes > 100 and title <> 'Admin') */ //Exists 語法 DB::table('users') ->whereExists(function($query) { $query->select(DB::raw(1)) ->from('orders') ->whereRaw('orders.user_id = users.id'); }) ->get(); /* 上面的查找語法會產生下方的 SQL: select * from users where exists ( select 1 from orders where orders.user_id = users.id ) */
whereRaw(能夠加原生語句):例如:
TrGoModel::query()->whereRaw('id = 286')->first();
$users = DB::table('users')->count(); $price = DB::table('orders')->max('price'); $price = DB::table('orders')->min('price'); $price = DB::table('orders')->avg('price'); $total = DB::table('users')->sum('votes');
$users = DB::table('users') ->select(DB::raw('count(*) as user_count, status')) ->where('status', '<>', 1) ->groupBy('status') ->get();
whereRaw(能夠加原生語句):例如:
TrGoModel::query()->whereRaw('id = 286')->first();
添加數據進數據表 DB::table('users')->insert( ['email' => 'john@example.com', 'votes' => 0] ); 添加自動遞增 (Auto-Incrementing) ID 的數據至數據表 若是數據表有自動遞增的ID,能夠使用 insertGetId 添加數據並返回該 ID: $id = DB::table('users')->insertGetId( ['email' => 'john@example.com', 'votes' => 0] ); 注意: 當使用 PostgreSQL 時,insertGetId 方法會預期自動增長的字段是以「id」爲命名。 添加多個數據進數據表 DB::table('users')->insert([ ['email' => 'taylor@example.com', 'votes' => 0], ['email' => 'dayle@example.com', 'votes' => 0] ]);
更新數據表中的數據 DB::table('users') ->where('id', 1) ->update(['votes' => 1]); 自增或自減一個字段的值 DB::table('users')->increment('votes'); DB::table('users')->increment('votes', 5); DB::table('users')->decrement('votes'); DB::table('users')->decrement('votes', 5); 也可以同時指定其餘要更新的字段: DB::table('users')->increment('votes', 1, ['name' => 'John']);
刪除數據表中的數據 DB::table('users')->where('votes', '<', 100)->delete(); 刪除數據表中的全部數據 DB::table('users')->delete(); 清空數據表 DB::table('users')->truncate();
查詢構造器也提供一個快速的方法去「合併 (union)」兩個查找的結果: $first = DB::table('users')->whereNull('first_name'); $users = DB::table('users')->whereNull('last_name')->union($first)->get(); unionAll 方法也能夠使用,它與 union 方法的使用方式同樣。
查詢構造器提供了少數函數協助你在 SELECT 語句中作到「悲觀鎖定」。 想要在 SELECT 語句中加上「Shard lock」,只要在查找語句中使用 sharedLock 函數: DB::table('users')->where('votes', '>', 100)->sharedLock()->get(); 要在 select 語法中使用「鎖住更新(lock for update)」時,你能夠使用 lockForUpdate方法: DB::table('users')->where('votes', '>', 100)->lockForUpdate()->get();
<?php $users = DB::table('users')->chunk(100, function(){ foreach($users as $user){ return false; // 返回false中止處理接下來的數據列 } }) ?>