Laravel查询构造器如何实现CURD操作,长尾词版本?

2026-04-02 06:041阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计677个文字,预计阅读时间需要3分钟。

Laravel查询构造器如何实现CURD操作,长尾词版本?

原文:本文字例讲述了Laravel框架查询构造器+CURD操作。分享给大众提供大众参考,具体如下:新增/插入一条数据public function insert() { $rs=DB::table('student')->insert(['name'=> 'Kit', 'age'=> 12]); dd($rs); }

改写后:Laravel框架使用查询构造器实现CURD操作实例。以下为新增数据操作代码:public function insert() { $rs=DB::table('student')->insert(['name'=> 'Kit', 'age'=> 12]); dd($rs); }

本文实例讲述了Laravel框架查询构造器 CURD操作。分享给大家供大家参考,具体如下:

新增

//插入一条数据 public function insert(){ $rs = DB::table('student')->insert([ 'name' => 'Kit', 'age' => 12 ]); dd($rs); //true }

//插入一条数据并返回自增ID public function insert(){ $id = DB::table('student')->insertGetId([ 'name'=>'Tom', 'age'=>11 ]); dd($id); //1004 }

//插入多条数据 public function insert(){ $rs = DB::table('student')->insert([ ['name'=>'Ben','age'=>22], ['name'=>'Jean','age'=>23] ]); dd($rs);//true }

更新

//更新一条数据 public function update(){ $rs = DB::table('student') ->where('id',1003) ->update(['age'=>10]); dd($rs);//1,返回受影响的行数 }

//自增更新 public function update(){ //所有年龄加1 $rs = DB::table('student')->increment('age'); dd($rs);//5,返回受影响的行数 //ID为1001的年龄加3 $rs = DB::table('student') ->where('id',1001) ->increment('age',3); dd($rs);//1,返回受影响的行数 }

//自减更新 public function update(){ //所有年龄加1 $rs = DB::table('student')->decrement('age'); dd($rs);//5,返回受影响的行数 //ID为1001的年龄加3 $rs = DB::table('student') ->where('id',1001) ->decrement('age',3); dd($rs);//1,返回受影响的行数 }

//1001年龄加3并且性别改为11 public function update(){ $rs = DB::table('student') ->where('id',1001) ->increment('age',3,['sex'=>11]); dd($rs);//1,返回受影响的行数 }

删除

//删除ID为1006的数据 public function delete(){ $rs = DB::table('student') ->where('id',1006) ->delete(); dd($rs);//1,返回受影响的行数 }

//删除ID大于1003的数据 public function delete(){ $rs = DB::table('student') ->where('id','>',1003) ->delete(); dd($rs);//2,返回受影响的行数 }

//清空数据表,不返回任何东西 DB::table('student')->truncate();

查询

  • get
  • first
  • pluck
  • select

//查询所有数据 $rs = DB::table('student')->get();

//查询第一条数据 $rs = DB::table('student')->orderBy('id','desc')->first();

//查询一个name字段 $rs = DB::table('student')->pluck('name'); //查询name字段并以ID为键名 $rs = DB::table('student')->pluck('name','id');

//查询name,age,sex字段 $rs = DB::table('student')->select('name','age','sex')->get();

聚合函数

$rs = DB::table('student')->count(); $rs = DB::table('student')->max('age'); $rs = DB::table('student')->min('age'); $rs = DB::table('student')->avg('age'); $rs = DB::table('student')->sum('age');

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

Laravel查询构造器如何实现CURD操作,长尾词版本?

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

本文共计677个文字,预计阅读时间需要3分钟。

Laravel查询构造器如何实现CURD操作,长尾词版本?

原文:本文字例讲述了Laravel框架查询构造器+CURD操作。分享给大众提供大众参考,具体如下:新增/插入一条数据public function insert() { $rs=DB::table('student')->insert(['name'=> 'Kit', 'age'=> 12]); dd($rs); }

改写后:Laravel框架使用查询构造器实现CURD操作实例。以下为新增数据操作代码:public function insert() { $rs=DB::table('student')->insert(['name'=> 'Kit', 'age'=> 12]); dd($rs); }

本文实例讲述了Laravel框架查询构造器 CURD操作。分享给大家供大家参考,具体如下:

新增

//插入一条数据 public function insert(){ $rs = DB::table('student')->insert([ 'name' => 'Kit', 'age' => 12 ]); dd($rs); //true }

//插入一条数据并返回自增ID public function insert(){ $id = DB::table('student')->insertGetId([ 'name'=>'Tom', 'age'=>11 ]); dd($id); //1004 }

//插入多条数据 public function insert(){ $rs = DB::table('student')->insert([ ['name'=>'Ben','age'=>22], ['name'=>'Jean','age'=>23] ]); dd($rs);//true }

更新

//更新一条数据 public function update(){ $rs = DB::table('student') ->where('id',1003) ->update(['age'=>10]); dd($rs);//1,返回受影响的行数 }

//自增更新 public function update(){ //所有年龄加1 $rs = DB::table('student')->increment('age'); dd($rs);//5,返回受影响的行数 //ID为1001的年龄加3 $rs = DB::table('student') ->where('id',1001) ->increment('age',3); dd($rs);//1,返回受影响的行数 }

//自减更新 public function update(){ //所有年龄加1 $rs = DB::table('student')->decrement('age'); dd($rs);//5,返回受影响的行数 //ID为1001的年龄加3 $rs = DB::table('student') ->where('id',1001) ->decrement('age',3); dd($rs);//1,返回受影响的行数 }

//1001年龄加3并且性别改为11 public function update(){ $rs = DB::table('student') ->where('id',1001) ->increment('age',3,['sex'=>11]); dd($rs);//1,返回受影响的行数 }

删除

//删除ID为1006的数据 public function delete(){ $rs = DB::table('student') ->where('id',1006) ->delete(); dd($rs);//1,返回受影响的行数 }

//删除ID大于1003的数据 public function delete(){ $rs = DB::table('student') ->where('id','>',1003) ->delete(); dd($rs);//2,返回受影响的行数 }

//清空数据表,不返回任何东西 DB::table('student')->truncate();

查询

  • get
  • first
  • pluck
  • select

//查询所有数据 $rs = DB::table('student')->get();

//查询第一条数据 $rs = DB::table('student')->orderBy('id','desc')->first();

//查询一个name字段 $rs = DB::table('student')->pluck('name'); //查询name字段并以ID为键名 $rs = DB::table('student')->pluck('name','id');

//查询name,age,sex字段 $rs = DB::table('student')->select('name','age','sex')->get();

聚合函数

$rs = DB::table('student')->count(); $rs = DB::table('student')->max('age'); $rs = DB::table('student')->min('age'); $rs = DB::table('student')->avg('age'); $rs = DB::table('student')->sum('age');

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

Laravel查询构造器如何实现CURD操作,长尾词版本?

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。