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
Laravel 入门笔记的更多相关文章
- Laravel入门笔记
Laravel 是一款简洁,优雅的一款框架,可以说是入门TP后的第二款可以选择的框架. 目录部分: app -> 自己写的代码 http -> Controller -> 控制器 b ...
- laravel教程入门笔记
安装laravel框架 1.安装命令 composer create-project --prefer-dist laravel/laravel ytkah ytkah表示文件夹名,如果不写的话自动会 ...
- CI框架入门笔记
当前(2019-03-22)CodeIgniter 框架的最新版本是 3.1.5,于2017年6月发布,距今快两年了也没有更新,这与 Laravel 的更新速度相比差距太大了.因为确实,它是一个很古老 ...
- 每天成长一点---WEB前端学习入门笔记
WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...
- ES6入门笔记
ES6入门笔记 02 Let&Const.md 增加了块级作用域. 常量 避免了变量提升 03 变量的解构赋值.md var [a, b, c] = [1, 2, 3]; var [[a,d] ...
- [Java入门笔记] 面向对象编程基础(二):方法详解
什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...
- React.js入门笔记
# React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...
- redis入门笔记(2)
redis入门笔记(2) 上篇文章介绍了redis的基本情况和支持的数据类型,本篇文章将介绍redis持久化.主从复制.简单的事务支持及发布订阅功能. 持久化 •redis是一个支持持久化的内存数据库 ...
- redis入门笔记(1)
redis入门笔记(1) 1. Redis 简介 •Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure serv ...
随机推荐
- MyEclipse打开JSP文件报"Failed to create the part's controls"解决方法汇总
有时候,打开别人的开发环境中导过来的项目的JSP文件,会出现“Failed to create the part's controls”的错误! 解决的方法有: 方法1:关闭myeclipse的jsp ...
- 并发包交换数据Exchanger
/** * * @描述: 用于实现两个人之间的数据交换,每个人完成一定的事务后想与对方交换数据,第一个先拿出数据的人一直等待 * 直到第二个人拿到数据 到来时,才能彼此交换数据. * @作者: Wnj ...
- 正则表达式 (python 2)
Python提供re模块,包含所有正则表达式的功能.由于Python的字符串本身也用\转义,所以要特别注意: s = 'ABC\\-001' # Python的字符串# 对应的正则表达式字符串变成:# ...
- oam系统安装,windows操作系统注册列表影响系统安装
windows注册列表可能会影响到系统的安装,本次安装oam10g版本,安装后没有问题,但是在配置oam和weblogic portal单点登录时在weblogic portal中访问oid和oam的 ...
- Geekforgeek week1
1. is palindrome solution 1: check to reverse the digit, if they are the same number https://www.gee ...
- pycharm 设置字体大写和显示代码行号
打开pycharm,我们看到左边是没有行号显示的. 在工具栏中点击扳手的标志,打开. 找到 Ide-setting ——>Editor ——>Apperance ——> ...
- iOS动画的要素:CALayer维护数据模型和图片,沟通了CPU和GPU--视图中与图形绘制相关的功能
1)iOS动画的模型:三层树模型: CALayer维护数据模型和图片,沟通了CPU和GPU:数据模型和图片本尊有CPU生成和维护:图片动画由GPU合成和呈现: https://developer.ap ...
- react 开发中的问题简记
1.什么时候用props 什么时候用state ? 不能使用props:当页面组件存在URL跳转问题时候,原因:若单独刷新,他会报错,拿不到前面的数据: 使用props场景:当组件为页面组件的一部分即 ...
- StringUtils工具类介绍
1 abbreviate方法缩写一段文字 StringUtils.abbreviate("abcdefghijklmno", -1, 10) = "abcdefg...& ...
- 文本编辑器js插件
1.首先引入所需js文件 <!--文本编辑器js组件--> <script type="text/javascript" charset="utf-8& ...