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 ...
随机推荐
- tomcat + jdk 快速部署
环境: apache-tomcat-7.0.73 java version "1.8.0_112" 注释:创建普通用户,使用 sudu进行操作,本文略写. 1.JDK 1.jdk配 ...
- css outline属性
outline属性在一个html element的周围画出一条线来,位于边框的外围,起到突出元素的作用.轮廓线不占据任何布局空间,也不一定非要是矩形. p { outline:#00FF00 dott ...
- phantomjs rendering
http://wwwy3y3.ghost.io/pageres-phantomjs-capture-sreenshot-chinese-fonts-not-render-correctly/ 在使用中 ...
- 【Leetcode】【Medium】Unique Binary Search Trees II
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- 在windows上安装nginx并注册
在windows上安装nginx并注册 一.前言 最近自己也尝试了一下在windows上安装nginx,其实非常的简单,这里算是备忘一下. 二.在windows下面安装 首先需要到nginx的 ...
- windows server 2003安装Oracle webtier 32位因环境变量原因报错
在服务中启动Oracle processer manager时报错:错误1053:服务没有及时响应启动或控制请求 原因是本系统还安装过BI和Oracle数据库等产品 解决方法:删除和本次安装无关的环境 ...
- 一个老IT产品管理者对于产品经理工作的经验总结
一个老IT产品管理者对于产品经理工作的经验总结 1. 任何行业,包括IT行业,所谓产品管理.产品经理,其核心应该是企业产品管理制度,也就是说,只有企业存在相应的.合理的产品管理体系和制度,产品管理.产 ...
- checkbox的readonly属性设置
方式一: checkbox没有readOnly属性,如果使用disabled=“disabled”属性的话,会让checkbox变成灰色的,用户很反感这种样式可以这样让它保持只读: 设置它的oncli ...
- 从零一起学Spring Boot之LayIM项目长成记(一) 初见 Spring Boot
项目背景 之前写过LayIM的.NET版后端实现,后来又写过一版Java的.当时用的是servlet,websocket和jdbc.虽然时间过去很久了,但是仍有些同学在关注.偶然间我听说了Spring ...
- 快速理解Event事件
浏览器事件是所有web程序的核心.javascript与HTML之间的交互是通过事件实现的.通过这些事件我们定义将要发生的行为.事件是一种异步编程的实现方式,本质上是程序各个组成部分之间的通信. 1. ...