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. java面试题之----jdbc中使用的设计模式(桥接模式)

    1.JDBC(JavaDatabase Connectivity) JDBC是以统一方式访问数据库的API. 它提供了独立于平台的数据库访问,也就是说,有了JDBC API,我们就不必为访问Oracl ...

  2. 如何阅读 Redis 源码?ZZ

    原文链接 在这篇文章中, 我将向大家介绍一种我认为比较合理的 Redis 源码阅读顺序, 希望可以给对 Redis 有兴趣并打算阅读 Redis 源码的朋友带来一点帮助. 第 1 步:阅读数据结构实现 ...

  3. Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止

    就这段时间,很多人在抱怨为什么自己的MySQL又打不开问题. 就“Windows(7)上不能启动MySQL服务(位于本地计算机上)错误1067 :进程意外终止”这个问题,我想到了几种方案解决: 一.首 ...

  4. Linux->apt-包的位置和变更

    ubuntu中由apt-get获得的文件包保存在/var/cache/apt/archives: 通过apt-get命令下载的软件包,放在/var/cache/apt/archives 目录下: 下载 ...

  5. java img图片转pdf 工具类

    package com.elitel.hljhr.comm.web.main.controller; import java.io.File; import java.io.FileNotFoundE ...

  6. sqlite 用法整理

    转载:http://blog.csdn.net/zhaoweixing1989/article/details/19080593 先纪录到这,以后慢慢整理. 1. 在Android下通过adb she ...

  7. BZOJ4887:[TJOI2017]可乐(矩阵乘法)

    Description 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且 放在了加里敦星球的1号城市上.这个可乐机器人有三种行为:停在原地,去下一个相邻的 城市,自爆. ...

  8. 「bzoj 3944: Sum」

    题目 杜教筛板子了 #include<iostream> #include<cstring> #include<cstdio> #include<cmath& ...

  9. PHP------知识复习

    PHP概述 (1)PHP(Hypertext  Perprocessor)超级文本预处理器 (2) PHP是一种在服务器端执行的嵌入HTML文档的脚本语言 (3) 是一种网站开发语言(B/S结构) ( ...

  10. PHP------文件------文件整体操作

    文件整体操作 [1]创建文件 touch("路径");   touch("./test.docx");//当前路径创建文件,创建的文档 显示的结果: touch ...