Laravel学习的基础知识

Laravel学习的基础知识

内容导读

收集整理的这篇技术教程文章主要介绍了Laravel学习的基础知识,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5484字,纯文字阅读大概需要8分钟

内容图文

这篇文章主要介绍了关于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 UserControllerphp 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<?phpnamespace App;use IlluminateDatabaseEloquentModel;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.phppublic 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的目录结构

以上就是Laravel 学习的基础知识的详细内容,更多请关注Gxl网其它相关文章!

内容总结

以上是为您收集整理的Laravel学习的基础知识全部内容,希望文章能够帮你解决Laravel学习的基础知识所遇到的程序开发问题。 如果觉得技术教程内容还不错,欢迎将网站推荐给程序员好友。

内容备注

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com