LARAVEL快速入门
2024-09-05 01:10:13
一、查询
$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();
最新文章
- Advanced REST client的使用说明
- Oracle 通过sql profile为sql语句加hint
- 火星A+B(字符串整形转化,进制)
- HDU 1203 I NEED A OFFER!(dp)
- Longest Palindromic Substring2015年6月20日
- RxJava(11-线程调度Scheduler)
- 使用HTML5抓取 Audio &; Video
- [RESTful] RESTful是什么,为什么要使用它
- GNum试用体验
- Jmeter如何提取响应头部的JSESSIONID
- 复习ing
- django模板语言的注释
- Java含有Date的对象序列化网络传输
- UVa 10763 Foreign Exchange(map)
- 2018.08.17 洛谷[POI2010]GRA-The Minima Game(线性dp)
- ubuntu16.04搭建hadoop集群环境
- UOJ #122 【NOI2013】 树的计数
- 开博缘由 , 可点下看看 http://www.cnblogs.com/jshare
- kendalltau肯德尔和谐系数
- JS中的“==”与强制类型转换