一.laravel中的sql語句集錦
1.插入資料
(1) DB::table('user')->insert($data);
(2)插入資料同時傳回自增id
DB::table('user')->insertGetId($data);
2.修改
DB::table('user')->where('id',1)->update($data);
3.删除
DB::table('user')->where('id',1)->delete();
4.查找一條資料
$res = AdminuserModel::where('id',$id)->first();
$res = AdminuserModel::find($id);//查詢id=1的資料
$pay_data=Db::table('config')->find(1);
5.擷取所有資料
PriceModel::get();擷取price表中所有資料
6.where or .Where Between;Where Not Between
(1)where
$users = DB::table('users')->where('votes', '>', 100)->get();
(2)or
$users = DB::table('users')->where('votes', '>', 100)->orWhere('name', 'John')->get();
(3)Where Between
$users = DB::table('users')->whereBetween('votes', array(1, 100))->get();
(4)Where Not Between
$users = DB::table('users')->whereNotBetween('votes', array(1, 100))->get();
(5)Where In
$users = DB::table('users')->whereIn('id', array(1, 2, 3))->get();
$users = DB::table('users')->whereNotIn('id', array(1, 2, 3))->get();
(6)Order By, Group By, And Having
$users = DB::table('users')->orderBy('name', 'desc')->groupBy('count')->having('count', '>', 100)->get();
(7)Offset & Limit
$users = DB::table('users')->offset($offset)->limit(10)->get();
7.laravel資料庫去除重複字段的内容(distinct)
$da=WlModel::groupby('name')->distinct()->get()->toarray();
// $da = DB::table('wl')->groupby('name')->distinct()->get();
二、連接配接
Joins
查詢建構器也可以用來編寫連接配接語句。看看下面的例子:
Basic Join Statement
複制代碼代碼如下:
DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.id', 'contacts.phone', 'orders.price')
->get();
左連接配接語句
複制代碼代碼如下:
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();
三、request相關用法
1.取到單個值 $request->name;同 $request->input('name');
$request->input('name', 'Linda'); 沒有擷取到用預設值
2.取到所有值 $request->all();
3.隻取哪些值 $request->only(['age','name']);
4.取出除了這些鍵的值 $request->except('name');
四、路由
//背景登入和退出
Route::any('/login','[email protected]')->name('login');
Route::any('/loginout','[email protected]')->name('loginout');
Route::group(['middleware' => ['login']], function () {//背景登入中間件
Route::any('/', '[email protected]')->name('index');
//使用者管理
Route::any('/admin/user/index','[email protected]');
});
// 比對[]裡面的路由資訊
Route::match(['get', 'post','put'], '/getPost', function () {
return 'Hello World';
});
//比對任何一個
Route::any('foo', function () {
return 'Hello World1';
});
五、分組
有時候,您可能需要建立更進階的where子句,如“存在”或嵌套參數分組。Laravel query builder可以處理這些:
複制代碼代碼如下:
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 Statements
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
上面的查詢将産生以下SQL:
複制代碼代碼如下:
select * from userswhere exists (
select 1 from orders where orders.user_id = users.id
)
六、聚合
查詢建構器還提供了各種聚合方法,如統計,馬克斯,min,avg和總和。
Using Aggregate Methods
複制代碼代碼如下:
$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');
Raw Expressions
有時您可能需要使用一個原始表達式的查詢。這些表達式将注入的查詢字元串,是以小心不要建立任何SQL注入點!建立一個原始表達式,可以使用DB:rawmethod:
Using A Raw Expression
複制代碼代碼如下:
$users = DB::table('users')
->select(DB::raw('count(*) as user_count, status'))
->where('status', '<>', 1)
->groupBy('status')
->get();
遞增或遞減一個列的值
複制代碼代碼如下:
DB::table('users')->increment('votes');
DB::table('users')->increment('votes', 5);
DB::table('users')->decrement('votes');
DB::table('users')->decrement('votes', 5);