דף עזר Laravel
מדריך Laravel לחיפוש והדפסה — Artisan, ניתוב, Eloquent, בונה השאילתות, מיגרציות, Blade, אימות ואוספים. בחינם.
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, ניתוב, קונטרולרים, ה-ORM של Eloquent, בנאי השאילתות, מיגרציות וסכימה, תבניות Blade, ולידציה, בקשות ותגובות, אוספים (collections), אימות ומידלוור, ועזרים.
ל-Laravel יש API עשיר אך רחב — החתימה המדויקת של קשר Eloquent, מאפיין עמודה במיגרציה, שם כלל ולידציה, שיטת אוסף — וכל שורה כאן מציגה את הקריאה האמיתית עם תיאור בשורה אחת, כך שהדף נקרא כמו קיצור של הפריימוורק.
הדף חופשי ומעובד לגמרי בדפדפן שלכם. סננו כל קטע קוד ישירות עם תיבת החיפוש, קפצו בין הסעיפים דרך תוכן העניינים הדביק, העתיקו קוד בלחיצה אחת והדפיסו את הדף לשימוש כרפרנס בזמן הפיתוח.
כיצד להשתמש ב-דף עזר Laravel
- עברו על שנים עשר הסעיפים, מ-Artisan CLI וניתוב ועד Eloquent ORM לעזרים שונים.
- חפשו מונח כמו migration, hasMany או validate כדי לסנן את הדף ישירות.
- קפצו לסעיף כמו תבניות Blade או אוספים דרך הסרגל הצידי הדביק.
- לחצו על קטע קוד או על סמל ההעתקה שלו כדי להעתיק את קוד ה-PHP לפרויקט שלכם.
- הדפיסו את הדף לשימוש כרפרנס Laravel לא מקוון.