laravel之数据库操作

  • 原生SQL语句操作
 插入  成功返回true,失败直接报错
        $data = DB::insert('insert into student(id,name,addr,phone) values(?,?,?,?)',[4,'Leslie','香港','12993']);
        $data = DB::insert('insert into student(name,addr,phone) values(:name,:addr,:phone)',['name'=>'Leslie','addr'=>'香港','phone'=>'12993']);
 修改 修改成功,返回受影响行数
        $data = DB::update('update student set name=?,addr=? where id=4',['孙双','东吴']);
        $data = DB::update('update student set name = :name,addr = :addr where id=2',['name'=>'刘玄德','addr'=>'川蜀']);
 查询
        $data = DB::select('select * from student where id=?',[1]);
        $data = DB::select('select * from student where name=:name',['name'=>'刘玄德']);
 删除 删除成功,返回受影响行数
        $data = DB::delete('delete from student where id=?',[1]);
        $data = DB::delete('delete from student where id=:id1 or id=:id2',['id1'=>2,'id2'=>3]);
  • 查询构造器操作SQL
  添加
        $data = DB::table('student')->insert(['name'=>'曹操','addr'=>'魏国']);
        $data = DB::table('student')->insert([['name'=>'刘备','addr'=>'川蜀'],['name'=>'诸葛亮','addr'=>'茅庐']]);

 修改
       $data = DB::table('student')->where('id',5)->update(['name'=>'曹操','addr'=>'大魏']);
 查询
        $data = DB::table('student')->select('name','addr','phone')->where('id',5)->get();
 删除
        $data = DB::table('student')->where('id',7)->delete();
上一篇:.Net中集合排序的一种高级玩法


下一篇:浏览器插件-离线英汉词典 0.0.7