Blogger Information
Blog 87
fans 1
comment 0
visits 59172
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel基础2
阿杰
Original
643 people have browsed it

laravel原生sql操作数据库

  • 配置数据库信息
  1. 原生查询操作

    在laravel框架里使用原生查询

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

    结果:

  2. 原生更改操作

    原生操作代码:

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

    结果:

  3. 原生新增操作

    1. // 原生新增操作
    2. public function inserts()
    3. {
    4. $res = DB::insert('insert into admin(username,password,ispasswd,gid,real_name) values("jack","djhsjjdf",1,2,"杰克")');
    5. var_dump($res);
    6. }

    结果:

  4. 原生删除操作

    1. // 原生删除操作
    2. public function dels()
    3. {
    4. $res = DB::delete('delete from admin where id=3');
    5. var_dump($res);
    6. }

laravel链式查询first方法

  • 查询第一条数据
    1. // 链式操作
    2. public function item()
    3. {
    4. // 链式调用
    5. $res = DB::table('admin')->first();
    6. echo '<pre>';
    7. print_r($res);
    8. }
  • 加了查询条件
    1. // 链式操作
    2. public function item()
    3. {
    4. // 链式调用
    5. $res = DB::table('admin')->where(['ispasswd'=>0])->first();
    6. echo '<pre>';
    7. print_r($res);
    8. }

总结

  • 初步了解laravel原生的增删改查操作数据库
  • 认识laravel链式查询first方法
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:有点应付了
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