zoukankan      html  css  js  c++  java
  • 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]
        );*/
    
    
    }
  • 相关阅读:
    什么是线程组,为什么在 Java 中不推荐使用?
    什么是 FutureTask?使用 ExecutorService 启动任务?
    Java 中用到的线程调度算法是什么?
    什么是阻塞队列?阻塞队列的实现原理是什么?如何使用 阻塞队列来实现生产者-消费者模型?
    说说对 SQL 语句优化有哪些方法?(选择几条)
    什么是 Executors 框架?
    Java Concurrency API 中的 Lock 接口(Lock interface) 是什么?对比同步它有什么优势?
    什么是原子操作?在 Java Concurrency API 中有哪些原 子类(atomic classes)?
    Java 中你怎样唤醒一个阻塞的线程?
    你将如何使用 thread dump?你将如何分析 Thread dump?
  • 原文地址:https://www.cnblogs.com/polax/p/13297052.html
Copyright © 2011-2022 走看看