Tài liệu tham khảo nhanh Laravel
Tài liệu tham khảo Laravel có thể tìm kiếm và in được — Artisan, routing, Eloquent, query builder, migration, Blade, xác thực và collection. Miễn phí.
Artisan CLI
14php artisan serve
php artisan make:model Post -mcr
php artisan make:controller PostController
php artisan make:migration create_posts_table
php artisan migrate
php artisan migrate:fresh --seed
php artisan db:seed
php artisan tinker
php artisan route:list
php artisan optimize
php artisan queue:work
php artisan schedule:work
php artisan storage:link
php artisan key:generate
Routing
13Route::get('/users', [UserController::class, 'index'])
Route::post('/users', [UserController::class, 'store'])
Route::put('/users/{user}', [UserController::class, 'update'])
Route::patch('/users/{user}', [UserController::class, 'update'])
Route::delete('/users/{user}', [UserController::class, 'destroy'])
Route::get('/users/{id}', $fn)
Route::get('/users/{id?}', $fn)
Route::get('/profile', $fn)->name('profile')
Route::get('/admin', $fn)->middleware('auth')
Route::resource('posts', PostController::class)
Route::prefix('admin')->group(function () {})
Route::controller(PostController::class)->group($fn)
Route::fallback($fn)
Controllers
10php artisan make:controller PostController --resource
public function __invoke(Request $request)
public function index() {}
public function store(Request $request) {}
public function show(Post $post) {}
public function __construct(PostService $svc) {}
return view('posts.index', ['posts' => $posts])
return response()->json($data)
return redirect()->route('posts.index')
return back()
Eloquent ORM
14Post::all()
Post::find($id)
Post::where('active', true)->first()
Post::create(['title' => 'Hi'])
$post->update(['title' => 'Edited'])
$post->delete()
Post::firstOrCreate(['slug' => $slug])
Post::updateOrCreate($attrs, $values)
Post::with('author')->get()
public function comments() { return $this->hasMany(Comment::class); }
public function author() { return $this->belongsTo(User::class); }
protected $fillable = ['title', 'body'];
protected function casts(): array { return ['published_at' => 'datetime']; }
Post::onlyTrashed()->restore()
Query builder
13DB::table('users')->get()
DB::table('users')->where('votes', '>', 100)->get()
DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')
DB::table('users')->orderBy('name')->get()
DB::table('orders')->groupBy('status')->get()
DB::table('users')->select('name', 'email')->get()
DB::table('users')->insert(['name' => 'Sam'])
DB::table('users')->where('id', 1)->update(['votes' => 1])
DB::table('users')->pluck('email')
DB::table('users')->count()
DB::table('users')->where('id', 1)->exists()
DB::table('users')->paginate(15)
DB::table('users')->chunk(100, $fn)
Migrations & schema
12Schema::create('posts', function (Blueprint $table) {})
$table->id()
$table->string('title')
$table->integer('votes')
$table->boolean('active')
$table->timestamps()
$table->foreignId('user_id')->constrained()
$table->string('note')->nullable()
$table->boolean('active')->default(true)
$table->index('slug')
$table->unique('email')
$table->dropColumn('votes')
Blade templates
14@if ($ok) ... @elseif ($x) ... @else ... @endif
@foreach ($posts as $post) ... @endforeach
@forelse ($posts as $post) ... @empty ... @endforelse
{{ $variable }}
{!! $html !!}
@extends('layouts.app')
@section('content') ... @endsection
@yield('content')
@include('partials.nav')
<x-alert type="error" />
@csrf
@auth ... @endauth
@can('update', $post) ... @endcan
{{ $loop->index }}
Validation
12$request->validate(['title' => 'required'])
'email' => 'required|email'
'name' => 'required|max:255'
'email' => 'unique:users,email'
'age' => 'nullable|integer|min:18'
'role' => ['required', Rule::in(['admin', 'user'])]
php artisan make:request StorePostRequest
public function rules(): array { return [...]; }
public function authorize(): bool { return true; }
public function messages(): array { return [...]; }
$validator = Validator::make($data, $rules)
$request->validated()
Requests & responses
10request()->input('name')
request()->query('page')
request()->all()
request()->only(['name', 'email'])
request()->has('name')
response()->json(['ok' => true])
redirect()->route('home')->with('status', 'Saved')
back()->withInput()
abort(404)
abort_if($user->banned, 403)
Collections
13$collection->map(fn ($x) => $x * 2)
$collection->filter(fn ($x) => $x > 0)
$collection->each(fn ($x) => $x->save())
$collection->pluck('name')
$collection->reduce(fn ($c, $x) => $c + $x, 0)
$collection->sortBy('created_at')
$collection->groupBy('status')
$collection->where('active', true)
$collection->first()
$collection->contains('name', 'Sam')
$collection->sum('price')
$collection->flatten()
$collection->toArray()
Auth & middleware
11Auth::user()
Auth::check()
Auth::id()
Auth::login($user)
Auth::logout()
auth()->user()
Route::get('/home', $fn)->middleware('auth')
Gate::allows('update', $post)
$user->can('update', $post)
php artisan make:policy PostPolicy --model=Post
php artisan make:middleware EnsureTokenIsValid
Helpers & misc
12config('app.name')
env('APP_DEBUG', false)
route('posts.show', $post)
url('/dashboard')
asset('css/app.css')
old('email')
now()->addDays(7)
Str::slug('My Title')
collect([1, 2, 3])->sum()
cache()->remember('key', 60, $fn)
dd($value)
Storage::put('file.txt', $contents)
Không có mục nào khớp với “:q”.
Giới thiệu về Tài liệu tham khảo nhanh Laravel
Bảng tra cứu Laravel này cô đọng những phần bạn chạm tới mỗi ngày trong framework: CLI Artisan, định tuyến, controller, Eloquent ORM, query builder, migration và schema, template Blade, xác thực dữ liệu, request và response, collection, xác thực người dùng và middleware, cùng các hàm trợ giúp.
Laravel có một API biểu cảm nhưng khá rộng — chữ ký chính xác của một quan hệ Eloquent, một modifier cột trong migration, tên một rule xác thực, một phương thức collection — và mỗi dòng ở đây hiển thị lời gọi thực tế kèm mô tả ngắn gọn, khiến bảng tra cứu đọc như phiên bản viết tắt của framework.
Bảng tra cứu này miễn phí và được render hoàn toàn trong trình duyệt của bạn. Lọc mọi đoạn mã ngay tại ô tìm kiếm, nhảy giữa các phần nhờ mục lục dính, sao chép mã chỉ với một cú nhấp và in bảng này để làm tài liệu tham khảo framework khi bạn xây dựng.
Cách sử dụng Tài liệu tham khảo nhanh Laravel
- Lướt qua mười hai phần, từ CLI Artisan và Định tuyến cho đến Eloquent ORM và Trợ giúp khác.
- Tìm một thuật ngữ như migration, hasMany hoặc validate để lọc bảng tra cứu ngay lập tức.
- Nhảy đến một phần như Template Blade hoặc Collection qua thanh bên dính.
- Nhấp vào một đoạn mã hoặc biểu tượng sao chép của nó để chép mã PHP vào dự án của bạn.
- In bảng tra cứu để có tài liệu Laravel offline.