0

0

Laravel 学习的基础知识

不言

不言

发布时间:2018-07-04 14:14:09

|

2768人浏览过

|

来源于php中文网

原创

这篇文章主要介绍了关于laravel 学习的基础知识,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

1.MVC简介

MVC全名是Model View Controller,是模型-视图-控制器的缩写Model是应用程序中用于处理应用程序数据逻辑的部分View是应用程序中处理数据显示的部分Controller是应用程序中处理用户交互的部分

2.laravel核心目录文件

目录

  • app包含了用户的核心代码

  • booststrap包含框架启动和配置加载文件

  • config包含所有的配置文件

  • database包含数据库填充与迁移文件

  • public包含项目入口可静态资源文件

  • resource包含视图与原始的资源文件

  • stroage包含编译后的模板文件以及基于文件的session和文件缓存、日志和框架文件

  • tests单元测试文件

  • wendor包含compose的依赖文件

    Liner
    Liner

    由ChatGPT驱动的AI辅助学习工具,将学习知识的速度提高 10 倍。

    下载

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

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网! 

 相关推荐:

laravel的目录结构

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Swift iOS架构设计与MVVM模式实战
Swift iOS架构设计与MVVM模式实战

本专题聚焦 Swift 在 iOS 应用架构设计中的实践,系统讲解 MVVM 模式的核心思想、数据绑定机制、模块拆分策略以及组件化开发方法。内容涵盖网络层封装、状态管理、依赖注入与性能优化技巧。通过完整项目案例,帮助开发者构建结构清晰、可维护性强的 iOS 应用架构体系。

23

2026.03.03

C++高性能网络编程与Reactor模型实践
C++高性能网络编程与Reactor模型实践

本专题围绕 C++ 在高性能网络服务开发中的应用展开,深入讲解 Socket 编程、多路复用机制、Reactor 模型设计原理以及线程池协作策略。内容涵盖 epoll 实现机制、内存管理优化、连接管理策略与高并发场景下的性能调优方法。通过构建高并发网络服务器实战案例,帮助开发者掌握 C++ 在底层系统与网络通信领域的核心技术。

25

2026.03.03

Golang 测试体系与代码质量保障:工程级可靠性建设
Golang 测试体系与代码质量保障:工程级可靠性建设

Go语言测试体系与代码质量保障聚焦于构建工程级可靠性系统。本专题深入解析Go的测试工具链(如go test)、单元测试、集成测试及端到端测试实践,结合代码覆盖率分析、静态代码扫描(如go vet)和动态分析工具,建立全链路质量监控机制。通过自动化测试框架、持续集成(CI)流水线配置及代码审查规范,实现测试用例管理、缺陷追踪与质量门禁控制,确保代码健壮性与可维护性,为高可靠性工程系统提供质量保障。

77

2026.02.28

Golang 工程化架构设计:可维护与可演进系统构建
Golang 工程化架构设计:可维护与可演进系统构建

Go语言工程化架构设计专注于构建高可维护性、可演进的企业级系统。本专题深入探讨Go项目的目录结构设计、模块划分、依赖管理等核心架构原则,涵盖微服务架构、领域驱动设计(DDD)在Go中的实践应用。通过实战案例解析接口抽象、错误处理、配置管理、日志监控等关键工程化技术,帮助开发者掌握构建稳定、可扩展Go应用的最佳实践方法。

60

2026.02.28

Golang 性能分析与运行时机制:构建高性能程序
Golang 性能分析与运行时机制:构建高性能程序

Go语言以其高效的并发模型和优异的性能表现广泛应用于高并发、高性能场景。其运行时机制包括 Goroutine 调度、内存管理、垃圾回收等方面,深入理解这些机制有助于编写更高效稳定的程序。本专题将系统讲解 Golang 的性能分析工具使用、常见性能瓶颈定位及优化策略,并结合实际案例剖析 Go 程序的运行时行为,帮助开发者掌握构建高性能应用的关键技能。

48

2026.02.28

Golang 并发编程模型与工程实践:从语言特性到系统性能
Golang 并发编程模型与工程实践:从语言特性到系统性能

本专题系统讲解 Golang 并发编程模型,从语言级特性出发,深入理解 goroutine、channel 与调度机制。结合工程实践,分析并发设计模式、性能瓶颈与资源控制策略,帮助将并发能力有效转化为稳定、可扩展的系统性能优势。

26

2026.02.27

Golang 高级特性与最佳实践:提升代码艺术
Golang 高级特性与最佳实践:提升代码艺术

本专题深入剖析 Golang 的高级特性与工程级最佳实践,涵盖并发模型、内存管理、接口设计与错误处理策略。通过真实场景与代码对比,引导从“可运行”走向“高质量”,帮助构建高性能、可扩展、易维护的优雅 Go 代码体系。

20

2026.02.27

Golang 测试与调试专题:确保代码可靠性
Golang 测试与调试专题:确保代码可靠性

本专题聚焦 Golang 的测试与调试体系,系统讲解单元测试、表驱动测试、基准测试与覆盖率分析方法,并深入剖析调试工具与常见问题定位思路。通过实践示例,引导建立可验证、可回归的工程习惯,从而持续提升代码可靠性与可维护性。

4

2026.02.27

漫蛙app官网链接入口
漫蛙app官网链接入口

漫蛙App官网提供多条稳定入口,包括 https://manwa.me、https

388

2026.02.27

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Node.js 教程
Node.js 教程

共57课时 | 12.6万人学习

CSS3 教程
CSS3 教程

共18课时 | 6.5万人学习

Rust 教程
Rust 教程

共28课时 | 6.5万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号