[TOC]
## Blade
```
// 輸出內容,被轉義過的
{{ $var }}
// 輸出未轉義內容
{!! $var !!}
{{-- Blade 注釋,不會被輸出到頁面中 --}}
// 三元表達式的簡寫,以下相當于「isset($name) ? $name : 'Default'」
{{ $name ?? 'Default' }}
// 等同 echo json_encode($array);
@json($array);
// 禁用 HTML 實體雙重編碼
Blade::withoutDoubleEncoding();
// 書寫 PHP 代碼
@php
@endphp
@csrf // CSRF 域
@method('PUT') // HTML 表單偽造方法 _method
// 服務容器注入,后調用 {{ $metrics->monthlyRevenue() }}
@inject('metrics', 'App\Services\MetricsService')
```
### 包含和繼承
```
// 擴展布局模板
@extends('layout.name')
// 區塊占位
@yield('name')
// 第一種、直接填入擴展內容
@section('title', 'Page Title')
// 第二種、實現命名為 name 的區塊(yield 占位的地方)
@section('sidebar')
// 繼承父模板內容
@parent
@endsection
// 可繼承內容區塊
@section('sidebar')
@show
// 繼承父模板內容(@show 的區塊內容)
@parent
// 包含子視圖
@include('view.name')
// 包含子視圖,并傳參
@include('view.name', ['key' => 'value']);
@includeIf('view.name', ['some' => 'data'])
@includeWhen($boolean, 'view.name', ['some' => 'data'])
// 包含給定視圖數組中第一個存在的視圖
@includeFirst(['custom.admin', 'admin'], ['some' => 'data'])
// 加載本地化語句
@lang('messages.name')
@choice('messages.name', 1);
// 檢查片斷是否存在
@hasSection('navigation')
@yield('navigation')
@endif
// 迭代 jobs 數組并包含
@each('view.name', $jobs, 'job')
@each('view.name', $jobs, 'job', 'view.empty')
// 堆棧
@stack('scripts')
@push('scripts')
<script src="/example.js"></script>
@endpush
// 棧頂插入
@prepend('scripts')
@endprepend
// 組件
@component('alert', ['foo' => 'bar'])
@slot('title')
@endslot
@endcomponent
// 注冊別名 @alert(['type' => 'danger'])...@endalert
Blade::component('components.alert', 'alert');
```
### 條件語句
```
@if (count($records) === 1)
@elseif (count($records) > 1)
@else
@endif
// 登錄情況下
@unless (Auth::check())
@endunless
// $records 被定義且不是 null...
@isset($records)
@endisset
// $records 為空...
@empty($records)
@endempty
// 此用戶身份已驗證...
@auth // 或 @auth('admin')
@endauth
// 此用戶身份未驗證...
@guest // 或 @guest('admin')
@endguest
@switch($i)
@case(1)
@break
@default
// 默認
@endswitch
```
### 循環
```
// for 循環
@for ($i = 0; $i < 10; $i++)
@endfor
// foreach 迭代
@foreach ($users as $user)
@endforeach
// 迭代如果為空的話
@forelse ($users as $user)
@empty
@endforelse
// while 循環
@while (true)
@endwhile
// 終結循環
@continue
@continue($user->type == 1) // 帶條件
// 跳過本次迭代
@break
@break($user->number == 5) // 帶條件
// 循環變量
$loop->index // 當前迭代的索引(從 0 開始計數)。
$loop->iteration // 當前循環迭代 (從 1 開始計算)。
$loop->remaining // 循環中剩余迭代的數量。
$loop->count // 被迭代的數組元素的總數。
$loop->first // 是否為循環的第一次迭代。
$loop->last // 是否為循環的最后一次迭代。
$loop->depth // 當前迭代的嵌套深度級數。
$loop->parent // 嵌套循環中,父循環的循環變量
```
### JavaScript 代碼
```
// JS 框架,保留雙大括號,以下會編譯為 {{ name }}
@{{ name }}
// 大段 JavaScript 變量,verbatim 里模板引擎將不解析
@verbatim
Hello, {{ javascriptVariableName }}.
@endverbatim
```
- 入門指南
- 安裝
- 部署
- 基礎功能
- 路由
- 中間件
- CSRF 保護
- 控制器
- 請求
- 響應
- 視圖
- URL
- Session
- 表單驗證
- 錯誤
- 日志
- 前端開發
- Blade 模板
- 本地化
- 腳手架
- 編譯資源 Mix
- 安全相關
- 用戶認證
- API 認證
- 綜合話題
- 命令行
- 廣播
- 緩存
- 集合
- 事件
- 文件存儲
- 輔助函數
- 郵件發送
- 消息通知
- 擴展包開發
- 隊列
- 任務調度
- 數據庫
- 快速入門
- 查詢構造器
- 分頁
- 數據庫遷移
- 數據填充
- Redis
- Eloquent ORM
- 快速入門
- 速查表
- Artisan
- Auth
- Blade
- Cache
- Collection
- Composer
- Config
- Container
- Cookie
- DB
- Environment
- Event
- File
- Helper
- Input
- Lang
- Log
- Model
- Pagination
- Queue
- Redirect
- Request
- Response
- Route
- SSH
- Schema
- Security
- Session
- Storage
- String
- URL
- UnitTest
- Validation
- View