小白兔晒黑了

导航

 

1 路由

\routes\web.php

<?php

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('/', 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 控制器

app\Http\Controllers\StudentController.php

<?php
/**
 * Created by PhpStorm.
 * User: SUN
 * Date: 2020/7/11
 * Time: 18:43
 */
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use App\Student;
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 模型

\app\Student.php

<?php
/**
 * Created by PhpStorm.
 * User: SUN
 * Date: 2020/7/11
 * Time: 17:06
 */
namespace App;
use Illuminate\Database\Eloquent\Model;
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]
    );*/


}

 

posted on 2020-07-14 07:57  小白兔晒黑了  阅读(242)  评论(0编辑  收藏  举报