laravel 数据库结构化查询

插入数据库

1
Companyauth::insert($data);

更新

1
Companyauth::where('uid',$uid)->update($data);

查询

1
Companyauth::where('uid',$uid)->find(1);

orderBy和limit组合

1
Companyauth::orderBy('id','desc')->take(6)->get();

where条件

1
2
Newsinformation::where('id','<',$id)->first();
Orderbook::where('buyId',$uid)->get();

left join

1
2
3
$prdList = Product::where('product.type',2)
->leftjoin('companyauth','companyauth.uid','=','product.uid')
->orderBy('product.id','desc')->get();

从配置文件读取数据

1
$choiseType = Config::get('user.choicetype');

单一纪录

1
Newsinformation::where('id','<',$id)->first();

连表查询、排序

1
2
3
4
5
6
7
8
9
10
11
12
$fields = [
'op.id','op.product_id','op.product_number as num',
'pd.image','op.name','op.order_id'
];

DB::table('order_product as op')
->select($fields)
->leftJoin('product as pd', 'pd.id', '=', 'op.product_id')
->where('op.order_id',$orderId)
->orderBy('id','desc')
->limit(6)
->get();

单一查询

1
2
3
4

DB::table('video')->select($fields)
->where('id',$vid)
->first();

自减

1
2
3
DB::table('product')
->where('id',$pid)
->decrement('gift_amount',$num);

更新

1
2
3
4
DB::table('product')
->where('id',$pid)
->where('amount','<=',0)
->update($fields);

添加返回id

```php
DB::table(‘comment’)
->insertGetId($fields);