为您找到搜索结果:343个
Laravel 修改操作
增加路由:Route::any('Student/update/{id}',['uses'=>'StudentController@update']);控制器代码:(Request$request不会影响参数)publicfunctionupdate(Request$request,$id){$student=Student::find($id);if($request->isMethod('POST')){//验证通过后继续进行//方法2Validator类验证$validator=Validator::make($request->input(),['Student.name'=>'required|min:2|max:20','Student.age'=>'required|integer','Student.gender'=>'required|integer',],['required'=>':attribute为必填项','min'=>':attribute长度不符合要求','integer'=>':attribute必...
Laravel 静态资源管理
<linkrel="stylesheet"href="{{asset('bootstrap/css/bootstrap.min.css')}}"/>asset()对应的路径是Public目录下。方式一:yield()方法在父模板中的使用父模板:@yield('content')子模板:@section('content')这里是具体内容@stop方法二:section()方法在父模板中的使用父模板:@section('content')父模板中的内容@show子模板:@section('content')这里是具体内容@stop结论:section() 方法可以在父模板中预定义内容。如果子模板也定义内容,则都显示。子模板中使用父级布局需要继承父级模板,使用extends方法,示例:@extends('common/layouts')子模板中需要引用其他页面时,使用include方法,示例:@include('common.message')根据当前页面判断左侧菜单显示样式<divclass="left-group"><aclass="list-g...
Laravel 表单及分页
控制器代码//列表publicfunctionindex(){//不带分页//$student=Student::get();//带分页$student=Student::paginate(5);returnview('Student.index',['students'=>$student]);}视图中代码<table><tr><td>ID</td><td>姓名</td><td>年龄</td><td>性别</td></tr>@foreach($studentsas$val)<tr><td>{{$val->id}}</td><td>{{$val->name}}</td><td>{{$val->age}}</td><td>{{$val->gender}}</td></tr>@endforeach</tab...
Laravel 表单验证
publicfunctioncreate(Request$request){if($request->isMethod('POST')){//验证通过后继续进行//方法1控制器验证$this->validate($request,['Student.name'=>'required|min:2|max:20','Student.age'=>'required|integer','Student.sex'=>'required|integer',],['required'=>':attribute为必填项','min'=>':attribute长度不符合要求','integer'=>':attribute必须是一个整形',],['Student.name'=>'姓名','Student.age'=>'年龄','Student.sex'=>'性别',]);//方法2Validator类验证$validator=Validator::make($request->input(),['Student.name'=>'...
Laravel 控制器的middleware中间件
场景:活动开始前只能访问宣传页面,开始后才可以访问其他页面:步骤:新建中间件,注册中间件,使用中间件,中间件的前置和后置操作。 控制器:publicfunctionactivity0(){return'活动快要开始啦,敬请期待';}publicfunctionactivity1(){return'活动页面1';}publicfunctionactivity2(){return'活动页面2';}路由:Route::any('activity0',['uses'=>'StudentController@activity0']);Route::any('activity1',['uses'=>'StudentController@activity1']);Route::any('activity2',['uses'=>'StudentController@activity2']);在AppHttpMiddleware下创建中间件Activity.php:namespaceAppHttpMiddleware;useClosure;classActivity{publ...
Laravel 控制器的response
publicfunctionresponse(){//响应json$data=['errCode'=>0,'errMsg'=>'success','data'=>'zhangsan'];returnresponse()->json($data);//重定向returnredirect('session2');//重定向,并附带信息returnredirect('session2')-with('message','我是快闪数据');//action()returnredirect()->action('StudentController@session2')-with('message','我是快闪数据');//route()returnredirect()->route('session2')-with('message','我是快闪数据');//back()返回上一个页面returnredirect()->back();}...
Laravel 控制器的session
设置路由//使用session,需要开启session,//session的开始类在/app/Kernel下//protected$middlewareGroups=[//'web'=>[//IlluminateSessionMiddlewareStartSession::class,//],//];Route::group(['middleware'=>['web']],function(){Route::any('session1',['uses'=>'StudentController@session1']);Route::any('session2',['uses'=>'StudentController@session2']);});//控制器里的方法publicfunctionsession1(Request$request){//1httprequestsession()$request->session()->put('key1','value1');echo$request->session()->get('key1')...
Laravel 控制器的request
publicfunctionrequest1(Request$request){//取值$name=Request::input('name');//是否有值if($request->has('name')){echo$request->input('name');}$res=$request->all();//判断请求类型echo$request->method();if($request->isMethod('POST')){echo'yes';}//判断是否是ajax$res=$request->ajax();//判断是否是某控制器下的方法$bool=$request->is('student/*');//获取当前的urlecho$request->url();}...
Laravel 视图中的url
<ahref="{{url('url')}}">url</a><ahref="{{action('StudentController@urlTest')}}">action</a><ahref="{{route('url')}}">route</a>...
Laravel 中视图中使用PHP代码
{{$name}}{{date('Y-m-dH:i:s',time())}}{{in_array($name,$arr)?'true':'false'}}{{isset($name)?$name:'default'}}{{$nameor'default'}}原样输出@{{$name}}@if($name=='hello')hello@elseif($name=='yes')yes@elsewho@endif@foreach($studentas$val){{$val->name}}@endforeach ...
Laravel 数据库操作之Eloquent ORM模型
//模型中的相关代码namespaceApp;useIlluminateDatabaseEloquentModel;classStudentextendsModel{//默认对应的是模型复数,即students,如果不是,需要自己指定表名protected$table='student';//指定表名//默认主键是id,如果不是,需要指定protected$primaryKey='id';//自动维护时间戳public$timestamps=true;protectedfunctiongetDateFormat(){returntime();}//不格式化时间戳protectedfunctionasDateTime($val){return$val;}//指定允许批量赋值的字段protected$fillable=['name','age'];//指定不允许批量赋值的字段protected$guarded=['name','age'];}//控制器的相关代码//orm使用模型查询publicfunctionorm1(){//all().返回模型的对象$student=Student::...
laravel 查询构造器2
//查询构造器publicfunctionquery(){//获取所有的数据$student=DB::table('student')->get();var_dump($student);//获取一条数据$student=DB::table('student')->first();var_dump($student);//使用where获取数据$student=DB::table('student')->where('id','>=',12)->get();var_dump($student);//使用多个where条件获取数据$student=DB::table('student')->whereRaw('id>=?andage>10',[2,18])->get();var_dump($student);//pluck返回结果集中指定的字段,列数据$student=DB::table('student')->whereRaw('id>=?andage>10',[2,18])->pluck('name');v...
laravel 查询构造器
//查询构造器publicfunctionquery(){$bool=DB::table('student')->insert([['name'=>'王五','age'=>17,'gender'=>1],['name'=>'王五2','age'=>17,'gender'=>1]]);var_dump($bool);//获取插入的数据的id$student_id=DB::table('student')->insertGetId(['name'=>'赵六','age'=>19,'gender'=>2]);var_dump($student_id);//更新数据,返回影响的行数$num=DB::table('student')->where('id',1)->update(['age'=>20,'gender'=>2]);var_dump($num);//更新数据,自增字段,返回影响的行数$num=DB::table('student')->where('id',1)->increment...
laravel 操作数据库
建立student控制器,控制器代码namespaceAppHttpControllers;useIlluminateSupportFacadesDB;classStudentControllerextendsController{//添加publicfunctionaddstudent(){$student=DB::insert('insertintostudent(name,age,gender)values(?,?,?)',['张三',12,2]);var_dump($student);//成功返回bloo值true}//获取publicfunctiongetall(){//$student=DB::select('select*fromstudent');$student=DB::select('select*fromstudentwhereid>?',[1]);return$student;//数组}//修改publicfunctionupdstudent(){$student=DB::update('updatestudentsetage=?wherename=?',...
laravel 数据库配置
数据库配置文件为项目根目录下的config/database.php //默认数据库为mysql'default'=>env('DB_CONNECTION','mysql'),'mysql'=>['driver'=>'mysql','host'=>env('DB_HOST','127.0.0.1'),'port'=>env('DB_PORT','3306'),'database'=>env('DB_DATABASE','forge'),'username'=>env('DB_USERNAME','forge'),'password'=>env('DB_PASSWORD',''),'unix_socket'=>env('DB_SOCKET',''),'charset'=>'utf8mb4','collation'=>'utf8mb4_unicode_ci','prefix'=>'','strict'=>true,'engine'=>null,], 发现都在调用env函数,找到env文件...