Laravel의 라우팅, 컨트롤러 및 모델

9001 단어

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 "
";
      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]
    );*/


}

 

좋은 웹페이지 즐겨찾기