• Laravel的路由、控制器和模型


    1 路由

    outesweb.php

    <?php
    
    use IlluminateSupportFacadesRoute;
    
    /*
    |--------------------------------------------------------------------------
    | 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('/', function () {
        return view('welcome');
    });
    /*Route::get('basic1',function (){
        return 'hello wor;d';
    });
    Route::get('user/{id}',function($id){
        return 'User-'.$id;
    });*/
    /*Route::get('user/{name}',function($name=null){
        return 'User-name-'.$name;
    });*/
    /*Route::get('user/name',['as'=>'name',function(){
        return  route('name');
    }]);
    Route::get('hello',function (){
        return view('welcome');
    });*/
    /*Route::get('member/{info?}','MemberController@info');*/
    Route::get('member/info',['uses' => 'MemberController@info']);
    
    Route::get('test1',['uses'=>'StudentController@test1']);
    Route::get('orm',['uses'=>'StudentController@orm']);

    2 控制器

    appHttpControllersStudentController.php

    <?php
    /**
     * Created by PhpStorm.
     * User: SUN
     * Date: 2020/7/11
     * Time: 18:43
     */
    namespace AppHttpControllers;
    use IlluminateSupportFacadesDB;
    use AppStudent;
    class StudentController extends Controller
    {
        public function test1()
        {
           /* $i = DB::insert('insert into student(name ,age) values (?,?)', ['tt',18]);
            $s = DB::select('select * from student');
            dd($s);*/
           //2 查询构造器
           /* $i = DB::table('student')->insert(
                ['name'=>'imoocaaa','age'=>43]
            );
            dd($i);*/
           /* $list = DB::table('student')->orderBy('id','desc')
                ->get();
            dd($list);*/
           /* $first = DB::table('student')->orderBy('id','desc')
                ->first();
            dd($first);*/
           /* $list = DB::table('student')
                ->where('age','<=',18)
                ->get();
            dd($list);*/
           //多个where的使用
           /* $list = DB::table('student')
                ->whereRaw('id >= ? and age >?',[1,18])
                ->get();
            dd($list);*/
            //pluck
            /*$name = DB::table('student')
                ->pluck('name');
                //->first();
            dd($name);*/
          /*  $name = DB::table('student')
                ->pluck('id','name');
            //->first();
            dd($name);*/
          /*$list = DB::table('student')
              ->select('id','name','age')
              ->get();
          dd($list);*/
         /*   echo "<pre>";
          DB::table('student')
              ->orderBy('id')
              ->chunk(2,function($students){
              var_dump($students);
              echo "-------------";
          });*/
          $num = DB::table('student')->count();
          var_dump($num);
          $min = DB::table('student')->min('age');
          $max = DB::table('student')->max('age');
          $count = DB::table('student')->count();
          $sum = DB::table('student')->sum('age');
    
          dd($sum);
    
        }
        public function orm()
        {
            /*$list = Student::all();
            dd($list);*/
    
            //新增数据
           /* $student = new Student();
            $student->name = 'sean';
            $student->age = '8';
            $bool = $student->save();
            dd($bool);*/
          /* $comment = Student::find(5);
           $comment->name = 'bbb';
           $comment->test = date('Y-m-d H:i:s',time());
           $bool  = $comment->save();
           echo($comment->updated_at);*/
          //使用模型的create方法新增数据
          /* $s = Student::create(['name'=>'imooc','age'=>18]);
           dd($s);*/
         /* $student = Student::firstOrCreate(
              ['name'=>'imoocaabb']
          );
          dd($student);*/
            /*$student = Student::firstOrNew(
                ['name'=>'imoocjjaabb']
            );
            $student->save();
            dd($student);*/
    
            /*$num = Student::where('id','>',9)->update(
                ['age'=>44]
            );
            var_dump($num);*/
           /* $bool= Student::where('id','>',9)->delete();
            dd($bool);*/
          /* $s = Student::find(8);
           $bool = $s->delete();
           dd($bool);*/
            $d = Student::destroy(7,11);
            dd($d);
        }
    }

    3 模型

    appStudent.php

    <?php
    /**
     * Created by PhpStorm.
     * User: SUN
     * Date: 2020/7/11
     * Time: 17:06
     */
    namespace App;
    use IlluminateDatabaseEloquentModel;
    class Student extends Model
    {
        //public $timestamps = false;
        protected $table = 'student';
        protected $primaryKey = 'id';
    
        //指定允许被批量赋值的字段
        //protected $fillable = ['name','age'];
        //指定不允许批量赋值的字段
        //protected $guarded = [];
    
        //protected $dateFormat = 'U';
        /*protected function asDateTime($val)
        {
            return $val;
        }*/
    
       /* DB::table('student')->insert(
            ['name'=>'tt','age'=>18]
        );*/
    
    
    }
  • 相关阅读:
    Week2实验 B--爆零(❌)大力出奇迹(√) HDU
    Windows10家庭版安装Docker(不是DockerToolbox)
    Ubuntu搭建DNS服务器遇到的问题1
    Ubuntu执行apt-get时遇到的问题1
    Windows2000下vc++6.0编译出现“Error spawning cl.exe”
    win2000安装官方发布的最后一个补丁KB891861
    Windows2000密钥
    Linux下使用gets和puts方法出现的错误
    执行.sh文件调用变量时出现“未找到命令”
    SQL Server2012无法连接到服务器
  • 原文地址:https://www.cnblogs.com/polax/p/13297052.html
Copyright © 2020-2023  润新知