批改状态:合格
老师批语:总结的很好, 操作数据库有多种方式, DB可以, 模型也很棒
1、路由案例
1.1代码:
Route::get('/db',function(){// dd($data);return DB::table('staff')->select('name','age')->get();});Route::get('/demo',function(){return view('demo');});Route::get('/hello',function(){return '欢迎访问页面';});Route::get('/sum/{a?}/{b?}',function ($a = 4, $b = 10) {return $a . '+' . $b . '=' . ($a + $b);});Route::get('/homecontroller/index','HomeController@index');Route::get('/homecontroller/db','HomeController@db');//数据库操作Route::get('/dbcontroller/db','DbController@db');//查询构造器Route::get('/dbcontroller/table','DbController@table');
1.2运行结果:
2、控制器
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use Illuminate\Support\Facades\DB;class HomeController extends Controller{public function index(){$name=$_GET['name'];$age=$_GET['age'];$user=[$name,$age];$data['username']=$user;// dd($data);return view('home/index1',$data);}public function db(){$data=DB::table('staff')->select('name','age')->get();// dd($data);$users['list']=$data;return view('home/index',$users);}}
3、数据库操作(代码):
<?phpnamespace App\Http\Controllers;use Illuminate\Support\Facades\DB;class DbController extends Controller{public function db(){// $n=$_GET['size'];// 查询// $data=DB::select('SELECT `staff_id`,`name`,`age`,`mobile` FROM `staff` LIMIT 0, :n',['n'=>$n]);// 更新操作$res=DB::update('update `staff` SET `age`=? WHERE `staff_id`=?',[18,1]);dd($res);// return $res;}//增加数据public function table(){$data = ['name'=> '东方不败','sex'=>1,'age'=>30,'salary'=>8000,'email'=>'dfbb@php.cn','mobile'=> '15800448811','password'=>sha1('123456'),'created_at'=>time(),'updated_at'=>time(),];$user= DB::table('staff')->insertGetId($data);dd($user);}//查询数据public function select(){$data= DB::table('staff')->select(['staff_id','name','age','mobile'])->get();dd($data);}//更新数据public function update(){$data= DB::table('staff')->where('staff_id',1)->update(['salary'=>10000]);dd($data);}//删除数据public function delete(){return DB::table('staff')->whereBetween('staff_id',[14,16])->delete();}}
一、laravel框架目录知识点:
1、控制器目录:blog\app\Http\Controllers;
2、网站根目录(www):public
3、路由目录:routes
4、视图目录:resources\views
5、环境配置文件:.evn
laravel中的定义输出函数dump();dd();输出后终止应用
二、路由相关知识:
1、将URL请求地址,映射到应用程序上的功能, 就是路由技术
2、Laravel中的Web请求默认路由文件是:routes/web.php,Laravel中的所有Web访问,必须设置路由才会生效
3、路由有路由动作和路由行为组成:例如:Route::get('hello', funciton (){...})
4、laravel中路由主要有web.php(url请求)和api.php(api接口请求)
5、路由的动作:GET,POST,PUT,PATCH,DELETE;
如果一个请求,需要响应多个动作,可以这样做:
// 可响应GET/POST请求Route::match(['get', 'post'], 'url', function () {...});// 可响应任何请求Route::any('url', function () {...});
6、路由参数:必选参数:{}标识;可选参数{?};参数类型约束:->where(参数,类型);链式调用
Route::get('sum/{a}/{b}', function ($a, $b) {return "$a + $b = " . ($a + $b);})->where(['a'=>'\d+', 'b'=>'\d+']);
7、闭包路由中(第二个参数必须是闭包)闭包函数的返回数据, 不要使用echo,应该统一使用return, 由响应对象决定如何返回前端;
8、路由第二个参数可以有控制器代替也成为控制路由;8、路由第二个参数可以有控制器代替;
三、数据库操作
1、原生查询:
1.2、操作数据库需要引用DB类:use Illuminate\Support\Facades\DB;
1、3操作方法:
增(insert):DB::insert(‘sql语句’,数据);返回true
删(delete): DB::delete(‘sql语句’,数据);返回受影响记录
改(update): DB::insert(‘sql语句’,数据);返回更新行数
查(select): DB::insert(‘sql语句’,数据);返回
sql语句:
查:SELECTid,name,emailFROMstaffsLIMIT 0, :n', ['n'=>$n]
更:'UPDATEstaffsSETage= ?,salary= ? WHEREid= ? ', [$age, $salary, $id]
增:'INSERTstaffs' . $fields . ' VALUES ' . $values;
删:'DELETE FROMstaffsWHEREid= ?', [$id]
删除更新必须有where条件
2、查询构造器
2.1:语句结构
DB::table(‘表名’)->insert(数据)/insertGetId();
DB::table(‘表名’)->where(条件)->update(数据组)
DB::table(‘表名’)->where(条件)->delete()
查询函数:get()、first()、value()、pluck(value,key)
3、字段封装:
select('id','name as 姓名','email')select(['id','name','email'])where(字段, 操作符, 值)whereBetween(字段, [起始, 结束])whereIn(字段, [值1,值2, ...])toSql(): 查看生成的SQL语句模板toArray(): 将结果对象集合转为数组
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号