Cet article présente principalement les connaissances de base sur l'apprentissage de Laravel, qui ont une certaine valeur de référence. Maintenant, je le partage avec tout le monde. Les amis dans le besoin peuvent s'y référer
Le nom complet de MVC est Model View Controller, qui est l'abréviation de Model-View-Controller2. >Model est la partie de l'application qui traite la logique des données de l'application
View est la partie de l'application qui traite l'affichage des données
Le contrôleur est la partie de l'application qui gère l'interaction de l'utilisateur
Route::match(['get', 'post']), 'match', funtion() { return 'match'; }); Route::any(['get', 'post']), funtion() { return 'any'; });
Route::get('user/{name}', funtion($name) { return $id; })->where('name', '[A-Za-z]+'); Route::get('user/{id}/{name?}', funtion($id, $name='phyxiao') { return $id. $name; })->where(['id' => '[0-9]+', 'name'=> '[A-Za-z]+']);
Route::get('user/home', ['as' => 'home', funtion() { return route('home'); }]);
Route::group(['prefix' => 'user'], funtion() { Route::get('home', funtion() { return 'home'; }); Route::get('about', funtion() { return 'about'; }); });
Route::get('index', funtion() { return view('welcome'); });
php artisan make:controller UserController php artisan make:controller UserController --plain
Route::get('index', 'UserController@index');
php artisan make:model User
fichiers associésORM éloquentconfig/base de données.php ,.envGénérateur de requêtes
$bool = DB::table('user')->insert(['name => phyxiao', 'age' => 18]); $id = DB::table('user')->insertGetId(['name => phyxiao', 'age' => 18]); $bool = DB::table('user')->insert([ ['name => phyxiao', 'age' => 18], ['name => aoteman', 'age' => 19], ); var_dump($bool);
$num= DB::table('user')->where('id', 12)->update(['age' => 30]); $num= DB::table('user')->increment('age', 3); $num= DB::table('user')->decrement('age', 3); $num= DB::table('user')->where('id', 12)->increment('age', 3); $num= DB::table('user')->where('id', 12)->increment('age', 3, ['name' =>'handsome']);
$num= DB::table('user')->where('id', 12)->delete(); $num= DB::table('user')->where('id', '>=', 12)->delete(); DB::table('user')->truncate();
$users= DB::table('user')->get(); $users= DB::table('user')->where('id', '>=', 12)->get(); $users= DB::table('user')->whereRaw('id >= ? and age > ?', [12, 18])->get(); dd(users); $user= DB::table('user')->orderBy('id', 'desc')->first(); $names = DB::table('user')->pluck('name'); $names = DB::table('user')->lists('name', 'id'); $users= DB::table('user')->select('id', 'age', 'name')->get(); $users= DB::table('user')->chunk(100, function($user){ dd($user); if($user->name == 'phyxiao') return false; });
$num= DB::table('user')->count(); $max= DB::table('user')->max('age'); $min= DB::table('user')->min('age'); $avg= DB::table('user')->avg('age'); $sum= DB::table('user')->avg('sum');
// 建立模型 // app/user.php <?php namespace App; use Illuminate\Database\Eloquent\Model; class User extends Model { //指定表名 protected $table = 'user'; //指定id protected $primaryKey= 'id'; //指定允许批量赋值的字段 protected $fillable= ['name', 'age']; //指定不允许批量赋值的字段 protected $guarded= []; //自动维护时间戳 public $timestamps = true; protected function getDateFormat() { return time(); } protected function asDateTime($val) { return val; } }
// ORM操作 // app/Http/Contollers/userController.php public function orm() { //all $students = Student::all(); //find $student = Student::find(12); //findOrFail $student = Student::findOrFail(12); // 结合查询构造器 $students = Student::get(); $students = Student::where('id', '>=', '10')->orderBy('age', 'desc')->first(); $num = Student::count(); //使用模型新增数据 $students = new Student(); $students->name = 'phyxiao'; $students->age= 18; $bool = $student->save(); $student = Student::find(20); echo date('Y-m-d H:i:s', $student->created_at); //使用模型的Create方法新增数据 $students = Student::create( ['name' => 'phyxiao', 'age' => 18] ); //firstOrCreate() $student = Student::firstOrCreate( ['name' => 'phyxiao'] ); //firstOrNew() $student = Student::firstOrNew( ['name' => 'phyxiao'] ); $bool= $student->save(); //使用模型更新数据 $student = Student::find(20); $student->name = 'phyxiao'; $student->age= 18; $bool = $student->save(); $num = Student::where('id', '>', 20)->update(['age' => 40]); //使用模型删除数据 $student = Student::find(20); $bool = $student->delete(); //使用主见删除数据 $num= Student::destroy(20); $num= Student::destroy([20, 21]); $num= Student::where('id', '>', 20)->delete; }
<!--展示某个section内容 占位符--> @yield('content', '内容') <!--定义视图片段--> @section(‘header’) 头部 @show
@extends('layouts') @section(‘header’) @parent header @stop @section(‘content’) content <!--模板输出php变量--> <p>{{$name}}</p> <!--模板调用php代码--> <p>{{ time() }}</p> <p>{{ date('Y-m-d H:i:s', time()) }}</p> <p>{{ in_array($name, $arr) ? 'true': 'false' }}</p> <p>{{ $name or 'default' }}</p> <!--原样输出--> <p>@{{$name}}</p> {{--模板注释--}} {{--引入子视图--}} @include('common', ['msg' => 'erro']) {{--流控制--}} @if ($name == 'phyxiao') I'm phyxiao @elseif($name == 'handsome') I'm handsome @else none @endif @unless($name == 'phyxiao') ture @endunless @for($i=0; $i < 10; $i++) {{$i}} @endfor @foreach($students as $student) {{$student->name}} @endfor @forelse($students as $student) {{$student->name}} @empty null @endforelse <a herf = "{{url('url')}}">text</a> <a herf = "{{action('UserController@index')}}">text</a> <a herf = "{{route('url')}}">text</a> @stop
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!