Laravel 学习的基础知识

不言
发布: 2018-07-04 14:14:09
原创
2751人浏览过

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

1.MVC简介

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

2.laravel核心目录文件

目录

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

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

  • config包含所有的配置文件

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

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

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

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

    无涯·问知
    无涯·问知

    无涯·问知,是一款基于星环大模型底座,结合个人知识库、企业知识库、法律法规、财经等多种知识源的企业级垂直领域问答产品

    无涯·问知 40
    查看详情 无涯·问知
  • 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
登录后复制

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

 相关推荐:

laravel的目录结构

以上就是Laravel 学习的基础知识的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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