這篇文章主要介紹了關於Laravel 學習的基礎知識,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
MVC全名是Model View Controller,是模型-視圖-控制器的縮寫
Model是應用程式中用於處理應用程式資料邏輯的部分
View是應用程式中處理資料顯示的部分
Controller是應用程式中處理使用者互動的部分
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
DB facode原始尋找####################################################################### ########查詢建構器###、查詢建構器 和Eloquent ORM#相關檔案config/database.php
、.env
$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
以上是Laravel 學習的基礎知識的詳細內容。更多資訊請關注PHP中文網其他相關文章!