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 ...
随机推荐
- salt 之 master and minion
系统:centos7.2 master:192.168.1.41minion:192.168.1.46 注释: setenforce 0 --关闭selinux systemctl stop fire ...
- redis3.2.8安装与简介
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久化,可以将内存中的 ...
- coder/programmer engineer Chirf Technology Offcer
大概是某个C轮融资的医疗网站CTO被离职.而CTO是一个知乎大V和微信大号.此事一出,在微信群有支持也有反对之声.支持此CTO被离职的认为其在工作时没有Review程序,自己不写代码,而是热衷出没于技 ...
- shell命令详解
sed命令 将文本input.txt中含有”姓名”字符串的行中的谢朝辉替换成扎巴依 sed -e '/姓名/s/谢朝辉/扎巴依/g' input.txt 将input.txt中第n(5)行替换成”ji ...
- Yii正则验证
required : 必须值验证属性 [['字段名'],required,'requiredValue'=>'必填值','message'=>'提示信息']; #说明:CRequiredV ...
- CDN缓存策略
以下内容就是FAQ,自己也学习一下... 1.CDN加速原理通过动态域名解析,网友的请求被分配到离自己最快的服务器.CDN服务器直接返回缓存文件或通过专线代理原站的内容.网络加速+内容缓存,有效提供访 ...
- 常规渗透:没遇到过的anquan狗
0x01 信息收集 服务器信息: windows 2003 + IIS 6.0 + aspx . Php + 安全狗 站点cms信息:一套aspx新闻发布系统 和 Discuz X3 端口信息 : 服 ...
- 20140322 卡迪夫城VS利物浦,拔出重剑,有惊无险
一.菱形442 起初在客战南安普顿的时候,罗杰斯启用了菱形442阵式,阵容和今天客战卡迪夫城几乎一样,只是格伦·约翰逊打左后卫,弗拉纳甘任职右后卫,目的是为了在客场抵御卢克·肖+拉拉纳.当时库蒂尼奥的 ...
- wampserver的安装与配置
一.安装:wamp的安装很简单,只需要按照提示并根据自己的需求操作即可,这里不再赘述. 二.配置:wamp安装完后,需进行如下配置才能正常工作. 1.修改MySQL的登录密码 (1)启动WampSer ...
- 怎么解决深入学习PHP的瓶颈?
PHP给学习者的感觉是:初学的时候很容易,但是学了2-3年,就深刻感觉遇到了瓶颈,很难深入,放弃又可惜.所谓“鸡肋,食之无味弃之可惜”的感觉很是贴切. 经常会有这种感觉:不学,看似也不后退:学了,好像 ...