gaocy

一、查询

$map = [];
$map[] = [\'u.store_id\',\'=\',0];
$map[] = [\'u.reg_time\',\'<\',time()];
$map[] = [\'u.user_rank\',\'in\',[0,9]];

DB::table(\'user_bonus as ub\')
    ->select(\'b.type_name\',\'b.discounts\',\'b.discounts_val\',\'b.use_end_time\',\'b.expire_days\',\'ub.status\')
    ->leftJoin(\'bonus as b\',\'b.type_id\',\'=\',\'ub.type_id\')
    ->where($map)
    ->groupBy(\'status\')
    ->offset(10)
    ->limit(20)
    ->orderBy(\'id\',\'desc\')
    ->get();


//从数据库表中获取一行数据,就使用 first 方法
$user = DB::table(\'users\')->where(\'name\', \'John\')->first();

//从记录中取出单个值
$email = DB::table(\'users\')->where(\'name\', \'John\')->value(\'email\');

//要获取包含单个字段值的集合,可以使用 pluck 方法。
$titles = DB::table(\'roles\')->pluck(\'title\');

//返回的集合中指定字段的自定义键值
$roles = DB::table(\'roles\')->pluck(\'title\', \'name\');

//获取记录数
$users = DB::table(\'users\')->count();

//原生sql查询
$users = DB::select(\'select * from users where active = ?\', [1]);

二、插入

//插入一条
DB::table(\'users\')->insert(
    [\'email\' => \'john@example.com\', \'votes\' => 0]
);
//插入多条
DB::table(\'users\')->insert([
    [\'email\' => \'taylor@example.com\', \'votes\' => 0],
    [\'email\' => \'dayle@example.com\', \'votes\' => 0]
]);

若数据表存在自增的 ID,则可以使用 insertGetId 方法来插入记录然后获取其 ID:
$id = DB::table(\'users\')->insertGetId(
    [\'email\' => \'john@example.com\', \'votes\' => 0]
);

//原生sql插入
DB::insert(\'insert into users (id, name) values (?, ?)\', [1, \'Dayle\']);

三、更新

//更新
DB::table(\'users\')
->where(\'id\', 1)
->update([\'votes\' => 1]);

//原生sql更新
$affected = DB::update(\'update users set votes = 100 where name = ?\', [\'John\']);

四、删除

DB::table(\'users\')->where(\'votes\', \'>\', 100)->delete();

//原生sql删除
$deleted = DB::delete(\'delete from users\');

 五、

//判断请求类型
$request->ajax();
$request->isMethod(\'GET\');
$request->isMethod(\'POST\');
 
//接受参数
$request->input(\'name\',\'\')

 六、SESSION

//设置session值
session()->put(\'key\',\'value\');

Session::put(\'key\',\'value\');

//把数组放到Session
Session::push(\'student\',\'gao\');
Session::push(\'student\',\'cong\');

//获取session值
session()->get(\'key\');

Session::get(\'key\');

Session::get(\'student\');

//取出所有值
Session::all();

//判断session某个值是否存在
Session::has(\'student\');

//删除session的某个值
Sessino:forget(\'student\');

 七、返回值、重定向

//返回json数据
return response()->json();

//重定向
return redirect()->with(\'message\',\'消息\');
return redirect()->action(\'UserController@login\')->with(\'message\',\'消息\');
return redirect()->route(\'路由别名\')->with(\'message\',\'消息\');
return redirect()->back();

 

分类:

技术点:

相关文章:

  • 2018-09-16
  • 2021-10-06
  • 2021-12-15
  • 2021-09-13
  • 2021-12-29
  • 2021-09-17
  • 2021-12-29
猜你喜欢
  • 2021-10-18
  • 2021-07-06
  • 2021-12-13
  • 2021-06-21
  • 2021-11-15
  • 2020-04-22
  • 2021-06-19
相关资源
相似解决方案