ชีตสรุป Laravel
เอกสารอ้างอิง Laravel ที่ค้นหาและพิมพ์ได้——Artisan, การกำหนดเส้นทาง, Eloquent, query builder, migration, Blade, การตรวจสอบความถูกต้อง และ collection ฟรี
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)
ไม่มีรายการที่ตรงกับ “:q”
เกี่ยวกับ ชีตสรุป Laravel
สรุปข้อมูล Laravel นี้ย่อพื้นผิวของเฟรมเวิร์กที่คุณสัมผัสทุกวันให้กระชับ ได้แก่ Artisan CLI, routing, controllers, Eloquent ORM, query builder, migrations และ schema, Blade templates, การตรวจสอบข้อมูล, requests และ responses, collections, auth และ middleware และ helpers
Laravel มี API ที่แสดงออกได้ดีแต่กว้างขวาง ไม่ว่าจะเป็นซิกเนเจอร์ที่แน่นอนของความสัมพันธ์ Eloquent, modifier ของคอลัมน์ใน migration, ชื่อกฎการตรวจสอบข้อมูล หรือเมธอดของ collection แต่ละแถวในนี้จะแสดงการเรียกใช้จริงพร้อมคำอธิบายสั้น ๆ หนึ่งบรรทัด ชีทนี้จึงอ่านได้เหมือนเฟรมเวิร์กแบบย่อ
ใช้งานฟรีและแสดงผลทั้งหมดในเบราว์เซอร์ของคุณ กรอง snippet ทุกตัวได้สดด้วยกล่องค้นหา กระโดดข้ามส่วนต่าง ๆ ได้จากสารบัญแบบ sticky คัดลอกโค้ดด้วยคลิกเดียว และพิมพ์ชีทเก็บไว้เป็นข้อมูลอ้างอิงของเฟรมเวิร์กระหว่างสร้างงาน
วิธีใช้ ชีตสรุป Laravel
- ไล่ดูสิบสองส่วน ตั้งแต่ Artisan CLI และ Routing ไปจนถึง Eloquent ORM และ Helpers & misc
- ค้นหาคำอย่าง migration, hasMany หรือ validate เพื่อกรองชีทแบบสด
- กระโดดไปยังส่วนต่าง ๆ เช่น Blade templates หรือ Collections ผ่านแถบด้านข้างแบบ sticky
- คลิกที่ snippet หรือไอคอนคัดลอกของมัน เพื่อคัดลอกโค้ด PHP ไปใส่ในโปรเจกต์ของคุณ
- พิมพ์ชีทนี้เก็บไว้เป็นข้อมูลอ้างอิง Laravel แบบออฟไลน์