เอกสารอ้างอิง Laravel ที่ค้นหาและพิมพ์ได้——Artisan, การกำหนดเส้นทาง, Eloquent, query builder, migration, Blade, การตรวจสอบความถูกต้อง และ collection ฟรี

Artisan CLI

14
php artisan serve
เริ่ม development server แบบ local
php artisan make:model Post -mcr
model พร้อม migration, controller และ resource
php artisan make:controller PostController
สร้างคลาส controller ใหม่
php artisan make:migration create_posts_table
สร้างไฟล์ migration ใหม่
php artisan migrate
รัน migration ฐานข้อมูลที่ค้างอยู่
php artisan migrate:fresh --seed
ลบทุกตาราง migrate ใหม่และ seed
php artisan db:seed
รัน seeder ฐานข้อมูล
php artisan tinker
เปิด REPL shell แบบโต้ตอบ
php artisan route:list
แสดง route ที่ลงทะเบียนทั้งหมด
php artisan optimize
cache config, route และ view
php artisan queue:work
ประมวลผล job ในคิว
php artisan schedule:work
รัน scheduler ใน foreground
php artisan storage:link
สร้าง symlink storage ไปยังพาธ public
php artisan key:generate
สร้าง application key

Routing

13
Route::get('/users', [UserController::class, 'index'])
นิยาม route GET ไปยัง controller
Route::post('/users', [UserController::class, 'store'])
นิยาม route POST
Route::put('/users/{user}', [UserController::class, 'update'])
นิยาม route PUT
Route::patch('/users/{user}', [UserController::class, 'update'])
นิยาม route PATCH
Route::delete('/users/{user}', [UserController::class, 'destroy'])
นิยาม route DELETE
Route::get('/users/{id}', $fn)
พารามิเตอร์ route ที่จำเป็น
Route::get('/users/{id?}', $fn)
พารามิเตอร์ route ที่ไม่บังคับ
Route::get('/profile', $fn)->name('profile')
ตั้งชื่อ route เพื่อสร้าง URL
Route::get('/admin', $fn)->middleware('auth')
ผูก middleware เข้ากับ route
Route::resource('posts', PostController::class)
ลงทะเบียน route resource แบบ RESTful
Route::prefix('admin')->group(function () {})
จัดกลุ่ม route ภายใต้ prefix ของ URI
Route::controller(PostController::class)->group($fn)
จัดกลุ่ม route ไปยัง controller เดียว
Route::fallback($fn)
จัดการ route ที่ไม่ match (404)

Controller

10
php artisan make:controller PostController --resource
resource controller พร้อม method CRUD
public function __invoke(Request $request)
controller แบบ invokable action เดียว
public function index() {}
แสดง resource (method resource)
public function store(Request $request) {}
บันทึก resource ใหม่
public function show(Post $post) {}
แสดง resource หนึ่งตัว (route model binding)
public function __construct(PostService $svc) {}
inject dependency ผ่าน constructor
return view('posts.index', ['posts' => $posts])
คืน Blade view พร้อมข้อมูล
return response()->json($data)
คืน response แบบ JSON
return redirect()->route('posts.index')
redirect ไปยัง named route
return back()
redirect ไปยังหน้าก่อนหน้า

Eloquent ORM

14
Post::all()
ดึงทุกเรคคอร์ด
Post::find($id)
หาเรคคอร์ดด้วย primary key
Post::where('active', true)->first()
เรคคอร์ดแรกที่ตรงเงื่อนไข
Post::create(['title' => 'Hi'])
Mass-assign และบันทึกเรคคอร์ด
$post->update(['title' => 'Edited'])
อัปเดต model ที่มีอยู่
$post->delete()
ลบ instance ของ model
Post::firstOrCreate(['slug' => $slug])
หาหรือสร้างเรคคอร์ดที่ตรงกัน
Post::updateOrCreate($attrs, $values)
อัปเดตหากพบ ไม่งั้นสร้าง
Post::with('author')->get()
Eager load ความสัมพันธ์
public function comments() { return $this->hasMany(Comment::class); }
ความสัมพันธ์แบบ one-to-many
public function author() { return $this->belongsTo(User::class); }
ความสัมพันธ์ one-to-many แบบผกผัน
protected $fillable = ['title', 'body'];
attribute ที่ mass-assign ได้
protected function casts(): array { return ['published_at' => 'datetime']; }
การ cast attribute (method casts())
Post::onlyTrashed()->restore()
กู้คืนเรคคอร์ดที่ soft-delete

Query builder

13
DB::table('users')->get()
ดึงทุกแถวจากตาราง
DB::table('users')->where('votes', '>', 100)->get()
กรองแถวตามเงื่อนไข
DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')
Inner join สองตาราง
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')
ดึงคอลัมน์เดียวเป็น collection
DB::table('users')->count()
นับแถวที่ตรงกัน
DB::table('users')->where('id', 1)->exists()
ตรวจสอบว่ามีแถวอยู่หรือไม่
DB::table('users')->paginate(15)
แบ่งหน้าผลลัพธ์
DB::table('users')->chunk(100, $fn)
ประมวลผลผลลัพธ์เป็นก้อน

Migration และ schema

12
Schema::create('posts', function (Blueprint $table) {})
สร้างตารางใหม่
$table->id()
primary key แบบเพิ่มอัตโนมัติ
$table->string('title')
คอลัมน์ VARCHAR
$table->integer('votes')
คอลัมน์ integer
$table->boolean('active')
คอลัมน์ boolean
$table->timestamps()
คอลัมน์ created_at และ updated_at
$table->foreignId('user_id')->constrained()
foreign key พร้อม constraint
$table->string('note')->nullable()
อนุญาตค่า NULL
$table->boolean('active')->default(true)
ตั้งค่าเริ่มต้น
$table->index('slug')
เพิ่ม index ให้คอลัมน์
$table->unique('email')
เพิ่ม unique constraint
$table->dropColumn('votes')
ลบคอลัมน์

เทมเพลต Blade

14
@if ($ok) ... @elseif ($x) ... @else ... @endif
การ render ตามเงื่อนไข
@foreach ($posts as $post) ... @endforeach
วนซ้ำบน collection
@forelse ($posts as $post) ... @empty ... @endforelse
วนซ้ำพร้อม fallback กรณีว่าง
{{ $variable }}
echo เอาต์พุตแบบ escape
{!! $html !!}
echo HTML ดิบแบบไม่ escape
@extends('layouts.app')
สืบทอดจาก layout
@section('content') ... @endsection
นิยาม section ของ layout
@yield('content')
ออก section ใน layout
@include('partials.nav')
include view อื่น
<x-alert type="error" />
render คอมโพเนนต์ Blade
@csrf
field CSRF token สำหรับฟอร์ม
@auth ... @endauth
แสดงเนื้อหาแก่ผู้ใช้ที่ยืนยันตัวตน
@can('update', $post) ... @endcan
การตรวจ gate ของ authorization
{{ $loop->index }}
ตัวแปร loop ภายใน @foreach

Validation

12
$request->validate(['title' => 'required'])
ตรวจสอบข้อมูล request แบบ inline
'email' => 'required|email'
กฎ required และอีเมลที่ถูกต้อง
'name' => 'required|max:255'
required พร้อมความยาวสูงสุด
'email' => 'unique:users,email'
ต้องไม่ซ้ำในตาราง
'age' => 'nullable|integer|min:18'
integer ไม่บังคับพร้อมค่าต่ำสุด
'role' => ['required', Rule::in(['admin', 'user'])]
จำกัดให้อยู่ในค่าที่อนุญาต
php artisan make:request StorePostRequest
สร้างคลาส Form Request
public function rules(): array { return [...]; }
กฎ validation ของ Form Request
public function authorize(): bool { return true; }
การ authorization ของ Form Request
public function messages(): array { return [...]; }
ข้อความ validation กำหนดเอง
$validator = Validator::make($data, $rules)
สร้าง validator ด้วยตนเอง
$request->validated()
ดึงเฉพาะ input ที่ผ่าน validation

Request และ response

10
request()->input('name')
ดึงค่า input
request()->query('page')
ดึงค่าจาก query string
request()->all()
ดึง input ทั้งหมดเป็น array
request()->only(['name', 'email'])
ดึง input บางส่วน
request()->has('name')
ตรวจสอบว่ามี input หรือไม่
response()->json(['ok' => true])
คืน response แบบ JSON
redirect()->route('home')->with('status', 'Saved')
redirect พร้อม flash message
back()->withInput()
redirect กลับโดยคง input เดิม
abort(404)
โยน HTTP exception
abort_if($user->banned, 403)
ยกเลิก request ตามเงื่อนไข

Collection

13
$collection->map(fn ($x) => $x * 2)
แปลงแต่ละรายการ
$collection->filter(fn ($x) => $x > 0)
เก็บรายการที่ตรงกัน
$collection->each(fn ($x) => $x->save())
รัน callback ต่อรายการ
$collection->pluck('name')
ดึงคอลัมน์เดียว
$collection->reduce(fn ($c, $x) => $c + $x, 0)
ลดเหลือค่าเดียว
$collection->sortBy('created_at')
เรียงตาม key
$collection->groupBy('status')
จัดกลุ่มรายการตาม key
$collection->where('active', true)
กรองตามคู่ key/value
$collection->first()
ดึงรายการแรก
$collection->contains('name', 'Sam')
ตรวจหารายการที่ตรงกัน
$collection->sum('price')
รวมผลคอลัมน์
$collection->flatten()
ทำให้ collection ซ้อนแบนราบ
$collection->toArray()
แปลงเป็น array ธรรมดา

Auth และ middleware

11
Auth::user()
ดึงผู้ใช้ที่ยืนยันตัวตน
Auth::check()
ตรวจสอบว่าผู้ใช้ล็อกอินอยู่หรือไม่
Auth::id()
ดึง ID ของผู้ใช้ที่ยืนยันตัวตน
Auth::login($user)
ล็อกอินผู้ใช้
Auth::logout()
ล็อกเอาต์ผู้ใช้ปัจจุบัน
auth()->user()
ตัวช่วยสำหรับผู้ใช้ปัจจุบัน
Route::get('/home', $fn)->middleware('auth')
ป้องกัน route ด้วย auth
Gate::allows('update', $post)
ตรวจ gate ของ authorization
$user->can('update', $post)
ตรวจสิทธิ์เทียบกับ policy
php artisan make:policy PostPolicy --model=Post
สร้าง authorization policy
php artisan make:middleware EnsureTokenIsValid
สร้างคลาส middleware

ตัวช่วยและอื่นๆ

12
config('app.name')
อ่านค่า config
env('APP_DEBUG', false)
อ่านตัวแปรสภาพแวดล้อม
route('posts.show', $post)
สร้าง URL ไปยัง named route
url('/dashboard')
สร้าง URL แบบเต็ม
asset('css/app.css')
URL สำหรับ asset สาธารณะ
old('email')
ดึง input เดิมที่ flash ไว้
now()->addDays(7)
timestamp Carbon ปัจจุบัน
Str::slug('My Title')
ตัวช่วย string (slugify)
collect([1, 2, 3])->sum()
สร้าง collection จาก array
cache()->remember('key', 60, $fn)
cache ค่าตามระยะเวลาหนึ่ง
dd($value)
dump ค่าแล้วหยุดทำงาน
Storage::put('file.txt', $contents)
เขียนไฟล์ลง storage

ไม่มีรายการที่ตรงกับ “:q”


แชร์สิ่งนี้
ต้องการความช่วยเหลือ?
พบปัญหากับเครื่องมือนี้หรือไม่? แจ้งทีมงานของเรา
รายงานปัญหา

เพิ่มเครื่องมือฟรีนี้ลงในเว็บไซต์ของคุณเอง — คัดลอกและวางโค้ดด้านล่าง