批改状态:合格
老师批语:作业标题不必太强调版本,其实各版本之间的差异仅仅体现在高级选项上, 而这些目前大家用不到
本文上可以和原生查询的文章https://www.php.cn/blog/detail/22003.html对比
Laravel 的数据库查询构造器为创建和运行数据库查询提供了一个方便的接口。它可用于执行应用程序中大部分数据库操作,且可在所有支持的数据库系统上运行。
Laravel 的查询构造器使用 PDO 参数绑定来保护您的应用程序免受 SQL 注入攻击。因此没有必要清理作为绑定传递的字符串。
注意:PDO 不支持绑定列名。因此,不能让用户通过输入来指定查询语句所引用的列名,包括 order by 字段等等。 如果必须要允许用户通过选择某些列来进行查询,请始终根据允许列的白名单来校验列名。
如果你只需要从数据表中获取一行数据,可以使用 first 方法。该方法返回一个 StdClass 对象:
演示地址:http://www.php520.vip/dbfirst
// 高级数据库查询的方法(链式调用)public function first() {$res=DB::table('article')->where('id','18')->first();echo '<pre>';print_r($res);}
该 get 方法返回一个包含 Illuminate\Support\Collection 的结果,其中每个结果都是 PHP StdClass 对象的一个实例。可以访问字段作为对象的属性来访问每列的值:
演示地址:http://www.php520.vip/dbgetall
// 查询所有数据public function getall() {$res=DB::table('article')->get();echo '<pre>';print_r($res);}
仅查询 select(‘指定字段名’),如果需要给字段改名字,可以通过字段名后面 as 新的字段名
演示地址:http://www.php520.vip/dbgetselect
// 查询指定字段public function getselect() {$res=DB::table('article')->select('cate_id as cid','title')->get();echo '<pre>';print_r($res);}
在构造 where 查询实例的中,使用 where 方法需要传递三个参数:第一个参数是列名,第二个参数是任意一个数据库系统支持的运算符,第三个是该列要比较的值。
演示地址:http://www.php520.vip/dbgetwhere
// 查询指定字段public function getselect() {$res=DB::table('article')->select('cate_id as cid','title')->get();echo '<pre>';print_r($res);}
where(‘字段’,’like’,’%内容%’)
演示地址:http://www.php520.vip/dbgetlike
// likepublic function getlike() {$res=DB::table('article')->where('title','like','%php%')->get();echo '<pre>';print_r($res);}
可以一起链式调用 where 约束,也可以在查询中添加 or 字句。 orWhere 方法和 where 方法接收的参数一样:
演示地址:http://www.php520.vip/dbgetor
// or 语句public function getor() {$res=DB::table('article')->where('cate_id','11')->orWhere('cate_id','5')->get()->all();echo '<pre>';print_r($res);}
演示地址:http://www.php520.vip/dbgettosql
// tosqlpublic function gettosql() {$res=DB::table('article')->where('cate_id','11')->orWhere('cate_id','5')->tosql();echo '<pre>';print_r($res);}
whereIn 方法验证字段的值必须存在指定的数组里
演示地址:http://www.php520.vip/dbgetwherein
// where in 查询public function getwherein() {$res=DB::table('article')->whereIn('id',[1,4,8])->get()->all();echo '<pre>';print_r($res);}
演示地址:http://www.php520.vip/dbgetjoin
// 链表查询public function getjoin() {$res=DB::table('article')->join('users','users.id','=','article.uid')->select('article.id','article.cate_id','article.title','users.username as nickname')->get()->all();echo '<pre>';print_r($res);}
avg(),sum(),min(),max(),count()演示地址:http://www.php520.vip/dbgetpvs
//计算平均值public function pvs() {$res=DB::table('article')->avg('pv');echo $res;//计算平均值// $avg=0;// foreach ($res as $key => $value) {// $avg +=$value->pv;// }// $avg=$avg/count($res);// echo '<pre>';// echo $avg;}
insert演示地址:http://www.php520.vip/dbinsert2
//增加数据public function insert2() {$res=DB::table('article')->insert(['uid'=>5,'cate_id'=>15,'title'=>"我最爱php了",'pv'=>260]);var_dump($res);}
insertGetId演示地址:http://www.php520.vip/dbinsert3
//增加数据并返回主键public function insert3() {$res=DB::table('article')->insertGetId(['uid'=>6,'cate_id'=>18,'title'=>"我最爱php了哈哈哈",'pv'=>520]);var_dump($res);}
演示地址:http://www.php520.vip/dbupdate2
//修改数据public function update2() {$res=DB::table('article')->where('id',12)->update(['title'=>"update测试"]);var_dump($res);}
演示地址:http://www.php520.vip/dbdelete2
//修改数据public function delete2() {$res=DB::table('article')->where('id',23>delete();var_dump($res);}
<?php/*** @Author: Nicola* @Date: 2020-06-04 17:35:44* @email: admin@nicola.com* @Last Modified by: Nicola* @Last Modified time: 2020-06-05 05:57:23*/// 命名空间:和目录对应// 类名称要和文件名称一致namespace App\Http\Controllers;//引入数据库DB类use Illuminate\Support\Facades\DB;/***继承类*/class Home extends Controller{public function index() {// return 'www.php.cn';// 输出一个视图(view是laravel内核定义好)$time=date('Y-m-d H:i:s');$res=DB::select('select * from article');// $name='nicola';// echo '<pre>';// $data=[];// $data['time']=$time;// $data['name']='laravel基础教学';$datas=[];foreach ($res as $key => $value) {$datas[]=(array)$value;}$data['result']=$datas;// echo '<pre>';// print_r($data);// echo $data;// return view('test',['time'=>$time,'name'=>$name]);return view('test',$data);// return view('test')->with('time',$time)->with('name',$name);}//数据库查询(原生)public function get(){//select * from article$res=DB::select('select * from article where id >5');echo '<pre>';print_r($res);}//数据库更新操作(原生)public function update() {$res=DB::update('update article set title="我是最爱php的" where id=3');var_dump($res);//返回更新的行数(0和1)}//数据库新增操作(原生)public function insert() {$res=DB::insert('insert article(`uid`,`cate_id`,`title`,`pv`) values(2,5,"我是最爱php的yoyoyo",520)');// exit(print_r($res));var_dump($res);//返回时布尔值(true/false)}//数据库删除操作(原生)public function delete() {$res=DB::delete('delete from article where id=6');var_dump($res);//返回更新的行数(0和1)}// 高级数据库查询的方法(链式调用)public function first() {$res=DB::table('article')->where('id','24')->first();echo $res->title;echo '<pre>';print_r($res);}// 查询所有数据public function getall() {$res=DB::table('article')->get();echo '<pre>';print_r($res);}// 查询指定字段public function getselect() {$res=DB::table('article')->select('cate_id as cid','title')->get();echo '<pre>';print_r($res);}// where语句public function getwhere() {$res=DB::table('article')->select('cate_id as cid','title')->where('cate_id',11)->get();echo '<pre>';print_r($res);}// likepublic function getlike() {$res=DB::table('article')->where('title','like','%php%')->get();echo '<pre>';print_r($res);}// or 语句public function getor() {$res=DB::table('article')->where('cate_id','11')->orWhere('cate_id','5')->get()->all();echo '<pre>';print_r($res);}// tosqlpublic function gettosql() {$res=DB::table('article')->where('cate_id','11')->orWhere('cate_id','5')->tosql();echo '<pre>';print_r($res);}// where in 查询public function getwherein() {$res=DB::table('article')->whereIn('id',[1,4,8])->get()->all();echo '<pre>';print_r($res);}// 链表查询public function getjoin() {$res=DB::table('article')->join('users','users.id','=','article.uid')->select('article.id','article.cate_id','article.title','users.username as nickname')->get()->all();echo '<pre>';print_r($res);}//计算平均值public function pvs() {$res=DB::table('article')->avg('pv');echo $res;//计算平均值// $avg=0;// foreach ($res as $key => $value) {// $avg +=$value->pv;// }// $avg=$avg/count($res);// echo '<pre>';// echo $avg;}//增加数据public function insert2() {$res=DB::table('article')->insert(['uid'=>5,'cate_id'=>15,'title'=>"我最爱php了",'pv'=>260]);var_dump($res);}//增加数据并返回主键public function insert3() {$res=DB::table('article')->insertGetId(['uid'=>6,'cate_id'=>18,'title'=>"我最爱php了哈哈哈",'pv'=>520]);var_dump($res);}//修改数据public function update2() {$res=DB::table('article')->where('id',12)->update(['title'=>"update测试"]);var_dump($res);}//删除数据public function delete2() {$res=DB::table('article')->where('id',18)->delete();var_dump($res);}}
<?php/*** @Author: Nicola* @Date: 2020-06-03 00:01:11* @email: admin@nicola.com* @Last Modified by: Nicola* @Last Modified time: 2020-06-05 05:20:40*/use Illuminate\Support\Facades\Route;/*|--------------------------------------------------------------------------| Web Routes|--------------------------------------------------------------------------|| Here is where you can register web routes for your application. These| routes are loaded by the RouteServiceProvider within a group which| contains the "web" middleware group. Now create something great!|*/// Route::get('路径','函数')Route::get('/', function () {return view('welcome');//视图 视图引擎// echo "欢迎光临php中文网";// echo date('Y-m-d H:i:s');});//访问控制器中某个类的方法,请求映射到控制器中Route::get('/home','Home@index');Route::get('/dbselect','Home@get');Route::get('/dbupdate','Home@update');Route::get('/dbinsert','Home@insert');Route::get('/dbdelete','Home@delete');Route::get('/dbfirst','Home@first');Route::get('/dbgetall','Home@getall');Route::get('/dbgetselect','Home@getselect');Route::get('/dbgetwhere','Home@getwhere');Route::get('/dbgetlike','Home@getlike');Route::get('/dbgetor','Home@getor');Route::get('/dbgettosql','Home@gettosql');Route::get('/dbgetwherein','Home@getwherein');Route::get('/dbgetjoin','Home@getjoin');Route::get('/dbgetpvs','Home@pvs');Route::get('/dbinsert2','Home@insert2');Route::get('/dbinsert3','Home@insert3');Route::get('/dbupdate2','Home@update2');Route::get('/dbdelete2','Home@delete2');
Laravel Query Builder 查询构造器(query builder)提供方便、流畅的接口,用来建立及执行数据库查找语法,使用 PDO 参数绑定,以保护应用程序免于 SQL 注入。因此传入的参数不需额外转义特殊字符,基本可以满足所有的数据库操作,而且在所有支持的数据库系统上都可以执行
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号