Home  >  Article  >  Backend Development  >  The basics of Laravel learning

The basics of Laravel learning

不言
不言Original
2018-07-04 14:14:092506browse

This article mainly introduces the basic knowledge about Laravel learning, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

1.MVC Introduction

The full name of MVC is Model View Controller, which is the abbreviation of Model-View-Controller
Model is the part of the application used to process application data logic
View is the part of the application that processes data display
Controller is the part of the application that handles user interaction

2.laravel core directory file

The basics of Laravel learning

  • app Contains the user's core code

  • booststrap contains framework startup and configuration loading files

  • config contains all configuration files

  • database contains database filling and migration files

  • public contains project entry and static resource files

  • resource contains views and original The resource file

  • stroage contains compiled template files as well as file-based session and file cache, log and framework files

  • tests unit tests File

  • wendor contains dependency files of compose

3. Routing

Multiple request routing

Route::match(['get', 'post']), 'match', funtion()
{
    return 'match';
});
Route::any(['get', 'post']),  funtion()
{
    return 'any';
});

Routing parameters

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]+']);

Routing alias

Route::get('user/home',  ['as' => 'home', funtion()
{
    return route('home');
}]);

Routing group

Route::group(['prefix' => 'user'], funtion()
{
    Route::get('home', funtion()
   {
    return 'home';
   });
    Route::get('about', funtion()
   {
    return 'about';
   });
});

Routing output view

Route::get('index',  funtion()
{
    return view('welcome');
});

4.Controller

Create control Device

php artisan make:controller UserController
php artisan make:controller UserController --plain

Route associated controller

Route::get('index',  'UserController@index');

5. Model

php artisan make:model User

6. Database

Three ways:DB facode Raw lookup , Query Builder and Eloquent ORM
Related filesconfig/database.php, .env

Query constructor

$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 = &#39;user&#39;;
    //指定id
    protected $primaryKey= &#39;id&#39;;
    //指定允许批量赋值的字段
    protected $fillable= [&#39;name&#39;, &#39;age&#39;];
    //指定不允许批量赋值的字段
    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(&#39;id&#39;, &#39;>=&#39;, &#39;10&#39;)->orderBy(&#39;age&#39;, &#39;desc&#39;)->first();
    $num = Student::count();


    //使用模型新增数据
    $students = new Student();
    $students->name = &#39;phyxiao&#39;;
    $students->age= 18;
    $bool = $student->save();

    $student = Student::find(20);
    echo date(&#39;Y-m-d H:i:s&#39;, $student->created_at);


    //使用模型的Create方法新增数据
    $students = Student::create(
        [&#39;name&#39; => &#39;phyxiao&#39;, &#39;age&#39; => 18]
    );
    //firstOrCreate()
    $student = Student::firstOrCreate(
        [&#39;name&#39; => &#39;phyxiao&#39;]
    );
    //firstOrNew()
    $student = Student::firstOrNew(
        [&#39;name&#39; => &#39;phyxiao&#39;]
    );
    $bool= $student->save();


    //使用模型更新数据
    $student = Student::find(20);
    $student->name = &#39;phyxiao&#39;;
    $student->age= 18;
    $bool = $student->save();

    $num = Student::where(&#39;id&#39;, &#39;>&#39;, 20)->update([&#39;age&#39; => 40]);


    //使用模型删除数据
    $student = Student::find(20);
    $bool = $student->delete();
    //使用主见删除数据
    $num= Student::destroy(20);
    $num= Student::destroy([20, 21]);

    $num= Student::where(&#39;id&#39;, &#39;>&#39;, 20)->delete;

}

7.Blade template engine

<!--展示某个section内容 占位符-->
@yield(&#39;content&#39;, &#39;内容&#39;)
<!--定义视图片段-->
@section(‘header’)
头部
@show
@extends(&#39;layouts&#39;)
@section(‘header’)
    @parent
    header
@stop
@section(‘content’)
    content
    <!--模板输出php变量-->
    <p>{{$name}}</p>
    <!--模板调用php代码-->
    <p>{{ time() }}</p>
    <p>{{ date(&#39;Y-m-d H:i:s&#39;, time()) }}</p>

    <p>{{ in_array($name, $arr) ? &#39;true&#39;: &#39;false&#39; }}</p>
    <p>{{ $name or &#39;default&#39; }}</p>

    <!--原样输出-->
    <p>@{{$name}}</p>

    {{--模板注释--}}

    {{--引入子视图--}}
    @include(&#39;common&#39;, [&#39;msg&#39; => &#39;erro&#39;])

    {{--流控制--}}
    @if ($name == &#39;phyxiao&#39;)
        I&#39;m phyxiao
    @elseif($name == &#39;handsome&#39;)
        I&#39;m handsome
    @else
        none
    @endif

    @unless($name == &#39;phyxiao&#39;)
        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(&#39;url&#39;)}}">text</a>
    <a herf = "{{action(&#39;UserController@index&#39;)}}">text</a>
    <a herf = "{{route(&#39;url&#39;)}}">text</a>

@stop

The above is the entire content of this article, I hope it will be useful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

laravel’s directory structure

The above is the detailed content of The basics of Laravel learning. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn