Blogger Information
Blog 87
fans 1
comment 0
visits 59193
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel_day3
阿杰
Original
413 people have browsed it

一、数据库

1、数据库的配置

2、使用DB语句先要引入DB类

  • 没引入会报错
    1. public function querys(){
    2. $res = DB::select('select * from admin');
    3. var_dump($res);
    4. }
  • 引入执行

3、laravel数据库原生查询及页面渲染

  1. public function querys(){
  2. $data['admin_list'] = DB::select('select * from admin');
  3. // echo '<pre>';
  4. // var_dump($res);
  5. // print_r($res);
  6. return view('querys',$data);
  7. }
  1. <body>
  2. @foreach($admin_list as $key=>$val)
  3. <div>用户名:{{$val->username}}</div>
  4. @endforeach
  5. </body>

二、数据库原生操作

1、查看数据

  1. // 原生查询操作
  2. public function querys(){
  3. $data['admin_list'] = DB::select('select * from admin limit 0,:n',['n'=>3]);
  4. // echo '<pre>';
  5. // var_dump($res);
  6. // print_r($res);
  7. return view('querys',$data);
  8. }

2、修改操作

  1. // 修改操作
  2. public function updates(){
  3. $res = DB::update('update admin set ispasswd=1 where id=12');
  4. var_dump($res);
  5. }


int(1)表示的是受影响的行数

3、新增操作

  1. // 新增操作
  2. public function inserts(){
  3. $res = DB::insert('insert into admin(username,password,real_name) values("mingzai","asdasdasdfasdf","明仔")');
  4. var_dump($res);
  5. }


4、删除操作

  1. // 删除操作
  2. public function deletes(){
  3. $res = DB::delete('delete from admin where id=:n',['n'=>33]);
  4. var_dump($res);
  5. }


三、链式操作

  • 比原生更简洁
  1. // 链式操作
  2. public function item(){
  3. // 原生
  4. $res = DB::select('select * from admin limit 0,:n',['n'=>1]);
  5. // 链式
  6. $res2 = DB::table('admin')->first();
  7. echo '<pre>';
  8. print_r($res);
  9. print_r($res2);
  10. }

  • 多条件查询
  1. // $res2 = DB::table('admin')->where('ispasswd',2)->where('gid','<>',10)->get();
  2. $res2 = DB::table('admin')->where(['ispasswd'=>2,'gid'=>0])->get();

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post