Laravel 学习的基础知识
Jul 04, 2018 pm 02:14 PM这篇文章主要介绍了关于Laravel 学习的基础知识,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
1.MVC简介
MVC全名是Model View Controller,是模型-视图-控制器的缩写
Model是应用程序中用于处理应用程序数据逻辑的部分
View是应用程序中处理数据显示的部分
Controller是应用程序中处理用户交互的部分
2.laravel核心目录文件
app包含了用户的核心代码
booststrap包含框架启动和配置加载文件
config包含所有的配置文件
database包含数据库填充与迁移文件
public包含项目入口可静态资源文件
resource包含视图与原始的资源文件
stroage包含编译后的模板文件以及基于文件的session和文件缓存、日志和框架文件
tests单元测试文件
wendor包含compose的依赖文件
3.路由
多请求路由
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'); });
登录后复制
4.控制器
创建控制器
php artisan make:controller UserController php artisan make:controller UserController --plain
登录后复制
路由关联控制器
Route::get('index', 'UserController@index');
登录后复制
5.模型
php artisan make:model User
登录后复制
6.数据库
三种方式: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');
登录后复制
Eloquent ORM
// 建立模型 // 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; }
登录后复制
7.Blade模板引擎
<!--展示某个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
登录后复制
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!
相关推荐:
以上是Laravel 学习的基础知识的详细内容。更多信息请关注PHP中文网其他相关文章!
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热门文章
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前
By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前
By 尊渡假赌尊渡假赌尊渡假赌
击败分裂小说需要多长时间?
3 周前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保护它?
3 周前
By DDD

热门文章标签

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)