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 入门笔记的更多相关文章

  1. Laravel入门笔记

    Laravel 是一款简洁,优雅的一款框架,可以说是入门TP后的第二款可以选择的框架. 目录部分: app -> 自己写的代码 http -> Controller -> 控制器 b ...

  2. laravel教程入门笔记

    安装laravel框架 1.安装命令 composer create-project --prefer-dist laravel/laravel ytkah ytkah表示文件夹名,如果不写的话自动会 ...

  3. CI框架入门笔记

    当前(2019-03-22)CodeIgniter 框架的最新版本是 3.1.5,于2017年6月发布,距今快两年了也没有更新,这与 Laravel 的更新速度相比差距太大了.因为确实,它是一个很古老 ...

  4. 每天成长一点---WEB前端学习入门笔记

    WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...

  5. ES6入门笔记

    ES6入门笔记 02 Let&Const.md 增加了块级作用域. 常量 避免了变量提升 03 变量的解构赋值.md var [a, b, c] = [1, 2, 3]; var [[a,d] ...

  6. [Java入门笔记] 面向对象编程基础(二):方法详解

    什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...

  7. React.js入门笔记

    # React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...

  8. redis入门笔记(2)

    redis入门笔记(2) 上篇文章介绍了redis的基本情况和支持的数据类型,本篇文章将介绍redis持久化.主从复制.简单的事务支持及发布订阅功能. 持久化 •redis是一个支持持久化的内存数据库 ...

  9. redis入门笔记(1)

    redis入门笔记(1) 1. Redis 简介 •Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure serv ...

随机推荐

  1. c++ inline 的位置不当导致的 无法解析的外部符号

    这几天编写代码碰到 无法解析的外部符号 visual studio. 在类中 inline 修饰符应该放在类函数定义的时候而不是声明的地方 即 // test.h 头文件 class A { publ ...

  2. js判断客户浏览器类型,版本

    在JS中判断浏览器的 类型,估计是每个编辑过页面的开发人员都遇到过的问题.在众多的浏览器产品中,IE.Firefox.Opera.Safari........众多品牌 却标准不一,因此时常需要根据不同 ...

  3. devexpress chart 散点图加载并分组显示(可以自定义颜色 同组中的点颜色相同)

    this.dChart.Diagram.Series.Clear();//清空图的内容 var groups = result.GroupBy(itm => itm["flag&quo ...

  4. U盘中毒了?教你如何删除System Volume Information这个顽固文件夹

    不得不说cmd命令很好用呢.最近我的U盘中毒了,格式化都删除不了System Volume Information这个顽固的文件夹,真心伤不起哇!还好现在解决了问题.看来以后得好好对待U盘,不能乱用了 ...

  5. C#中的多线程 - 并行编程 z

    原文:http://www.albahari.com/threading/part5.aspx 专题:C#中的多线程 1并行编程Permalink 在这一部分,我们讨论 Framework 4.0 加 ...

  6. yii2.0用gii自动补全代码做的简单增删改查,以及图片上传和展示

    首先已经用gii根据model层生成了控制器,模型,视图层. 表结构为如图所示:表名为zhoukao1,

  7. RabbitMQ Windows环境搭建

    1.0 RabbitMQ是用Erlang语言编写,因此安装RabbitMQ,首先要安装Erlang. Erlang的官网:http://www.erlang.org/ 文件:otp_win64_19. ...

  8. BZOJ2333:[SCOI2011]棘手的操作(Splay)

    Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 x v: ...

  9. 13、SpringBoot-CRUD员工修改操作/删除

      对于修改连接的uri 在list.html中 <a class="btn btn-sm btn-primary" th:href="@{/update/} + ...

  10. sort论文和代码解读

    流程:1.detections和trackers用匈牙利算法进行匹配 2.把匹配中iou < 0.3的过滤成没匹配上的(1.2步共同返回匹配上的,没匹配上的trackers,没匹配上的detec ...