Laravel चीट शीट
खोजने और प्रिंट करने योग्य Laravel संदर्भ — Artisan, रूटिंग, Eloquent, क्वेरी बिल्डर, माइग्रेशन, Blade, वैलिडेशन और कलेक्शन। निःशुल्क।
Artisan CLI
14php artisan serve
लोकल development server शुरू करें
php artisan make:model Post -mcr
migration, controller और resource वाला Model
php artisan make:controller PostController
नई controller क्लास उत्पन्न करें
php artisan make:migration create_posts_table
नई migration फ़ाइल बनाएं
php artisan migrate
लंबित database migration चलाएं
php artisan migrate:fresh --seed
सभी table हटाएं, फिर migrate और seed करें
php artisan db:seed
database seeder चलाएं
php artisan tinker
इंटरैक्टिव REPL shell खोलें
php artisan route:list
सभी पंजीकृत route सूचीबद्ध करें
php artisan optimize
config, route और view कैश करें
php artisan queue:work
queue पर job प्रोसेस करें
php artisan schedule:work
scheduler को foreground में चलाएं
php artisan storage:link
storage को public path से symlink करें
php artisan key:generate
application key उत्पन्न करें
Routing
13Route::get('/users', [UserController::class, 'index'])
controller को GET route परिभाषित करें
Route::post('/users', [UserController::class, 'store'])
POST route परिभाषित करें
Route::put('/users/{user}', [UserController::class, 'update'])
PUT route परिभाषित करें
Route::patch('/users/{user}', [UserController::class, 'update'])
PATCH route परिभाषित करें
Route::delete('/users/{user}', [UserController::class, 'destroy'])
DELETE route परिभाषित करें
Route::get('/users/{id}', $fn)
आवश्यक route पैरामीटर
Route::get('/users/{id?}', $fn)
वैकल्पिक route पैरामीटर
Route::get('/profile', $fn)->name('profile')
URL निर्माण हेतु route का नाम दें
Route::get('/admin', $fn)->middleware('auth')
route में middleware संलग्न करें
Route::resource('posts', PostController::class)
RESTful resource route पंजीकृत करें
Route::prefix('admin')->group(function () {})
URI prefix के तहत route समूहित करें
Route::controller(PostController::class)->group($fn)
route को एक controller में समूहित करें
Route::fallback($fn)
बेमेल route संभालें (404)
Controller
10php artisan make:controller PostController --resource
CRUD method वाला resource controller
public function __invoke(Request $request)
एकल-क्रिया invokable controller
public function index() {}
resource सूचीबद्ध करें (resource method)
public function store(Request $request) {}
नया resource सहेजें
public function show(Post $post) {}
एक resource दिखाएं (route model binding)
public function __construct(PostService $svc) {}
constructor से dependency inject करें
return view('posts.index', ['posts' => $posts])
डेटा के साथ Blade view लौटाएं
return response()->json($data)
JSON प्रतिक्रिया लौटाएं
return redirect()->route('posts.index')
नामित route पर redirect करें
return back()
पिछले पेज पर redirect करें
Eloquent ORM
14Post::all()
हर record प्राप्त करें
Post::find($id)
primary key से record खोजें
Post::where('active', true)->first()
शर्त से मेल खाता पहला record
Post::create(['title' => 'Hi'])
record को mass-assign करें और सहेजें
$post->update(['title' => 'Edited'])
मौजूदा model अपडेट करें
$post->delete()
model instance हटाएं
Post::firstOrCreate(['slug' => $slug])
मिलान record खोजें या बनाएं
Post::updateOrCreate($attrs, $values)
मिले तो अपडेट करें, अन्यथा बनाएं
Post::with('author')->get()
relationship को eager load करें
public function comments() { return $this->hasMany(Comment::class); }
One-to-many relationship
public function author() { return $this->belongsTo(User::class); }
व्युत्क्रम one-to-many relationship
protected $fillable = ['title', 'body'];
Mass-assignable attribute
protected function casts(): array { return ['published_at' => 'datetime']; }
Attribute cast (casts() method)
Post::onlyTrashed()->restore()
soft-delete किए record पुनर्स्थापित करें
Query builder
13DB::table('users')->get()
table से सभी row लाएं
DB::table('users')->where('votes', '>', 100)->get()
शर्त से row फ़िल्टर करें
DB::table('users')->join('posts', 'users.id', '=', 'posts.user_id')
दो table को inner join करें
DB::table('users')->orderBy('name')->get()
column के अनुसार परिणाम क्रमित करें
DB::table('orders')->groupBy('status')->get()
column के अनुसार row समूहित करें
DB::table('users')->select('name', 'email')->get()
विशिष्ट columns चुनें
DB::table('users')->insert(['name' => 'Sam'])
नई row डालें
DB::table('users')->where('id', 1)->update(['votes' => 1])
मेल खाने वाली rows अपडेट करें
DB::table('users')->pluck('email')
एक column को collection के रूप में प्राप्त करें
DB::table('users')->count()
मिलान row गिनें
DB::table('users')->where('id', 1)->exists()
जांचें कि row मौजूद हैं या नहीं
DB::table('users')->paginate(15)
परिणाम paginate करें
DB::table('users')->chunk(100, $fn)
परिणाम chunk में प्रोसेस करें
Migration और schema
12Schema::create('posts', function (Blueprint $table) {})
नई table बनाएँ
$table->id()
Auto-incrementing primary key
$table->string('title')
VARCHAR column
$table->integer('votes')
Integer column
$table->boolean('active')
Boolean column
$table->timestamps()
created_at और updated_at column
$table->foreignId('user_id')->constrained()
constraint के साथ foreign key
$table->string('note')->nullable()
NULL मान अनुमति दें
$table->boolean('active')->default(true)
डिफ़ॉल्ट मान सेट करें
$table->index('slug')
column में index जोड़ें
$table->unique('email')
unique constraint जोड़ें
$table->dropColumn('votes')
column हटाएं
Blade template
14@if ($ok) ... @elseif ($x) ... @else ... @endif
सशर्त rendering
@foreach ($posts as $post) ... @endforeach
collection पर लूप करें
@forelse ($posts as $post) ... @empty ... @endforelse
खाली fallback के साथ लूप
{{ $variable }}
escaped आउटपुट echo करें
{!! $html !!}
unescaped raw HTML echo करें
@extends('layouts.app')
layout से प्राप्त करें
@section('content') ... @endsection
layout section परिभाषित करें
@yield('content')
layout में section आउटपुट करें
@include('partials.nav')
एक और view include करें
<x-alert type="error" />
Blade component render करें
@csrf
फ़ॉर्म हेतु CSRF token फ़ील्ड
@auth ... @endauth
प्रमाणित user को सामग्री दिखाएं
@can('update', $post) ... @endcan
Authorization gate जांच
{{ $loop->index }}
@foreach के अंदर loop वेरिएबल
सत्यापन
12$request->validate(['title' => 'required'])
अनुरोध डेटा inline सत्यापित करें
'email' => 'required|email'
आवश्यक और वैध email नियम
'name' => 'required|max:255'
अधिकतम लंबाई के साथ आवश्यक
'email' => 'unique:users,email'
table में unique होना चाहिए
'age' => 'nullable|integer|min:18'
न्यूनतम के साथ वैकल्पिक integer
'role' => ['required', Rule::in(['admin', 'user'])]
अनुमत मानों तक सीमित करें
php artisan make:request StorePostRequest
Form Request क्लास उत्पन्न करें
public function rules(): array { return [...]; }
Form Request सत्यापन नियम
public function authorize(): bool { return true; }
Form Request authorization
public function messages(): array { return [...]; }
कस्टम सत्यापन संदेश
$validator = Validator::make($data, $rules)
validator मैन्युअल रूप से बनाएं
$request->validated()
केवल सत्यापित input प्राप्त करें
अनुरोध और प्रतिक्रियाएं
10request()->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])
JSON प्रतिक्रिया लौटाएं
redirect()->route('home')->with('status', 'Saved')
flash संदेश के साथ redirect करें
back()->withInput()
पुराना input रखते हुए वापस redirect करें
abort(404)
HTTP exception फेंकें
abort_if($user->banned, 403)
सशर्त रूप से अनुरोध रद्द करें
Collection
13$collection->map(fn ($x) => $x * 2)
प्रत्येक आइटम रूपांतरित करें
$collection->filter(fn ($x) => $x > 0)
मिलान आइटम रखें
$collection->each(fn ($x) => $x->save())
प्रति आइटम callback चलाएं
$collection->pluck('name')
एक column निकालें
$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')
column का योग करें
$collection->flatten()
nested collection को समतल करें
$collection->toArray()
सादे array में बदलें
Auth और middleware
11Auth::user()
प्रमाणित user प्राप्त करें
Auth::check()
जांचें कि user लॉग इन है या नहीं
Auth::id()
प्रमाणित user ID प्राप्त करें
Auth::login($user)
user को लॉग इन करें
Auth::logout()
वर्तमान user को लॉग आउट करें
auth()->user()
वर्तमान user हेतु helper
Route::get('/home', $fn)->middleware('auth')
auth से route की रक्षा करें
Gate::allows('update', $post)
authorization gate जांचें
$user->can('update', $post)
policy के विरुद्ध क्षमता जांचें
php artisan make:policy PostPolicy --model=Post
authorization policy उत्पन्न करें
php artisan make:middleware EnsureTokenIsValid
middleware क्लास उत्पन्न करें
Helper और विविध
12config('app.name')
config मान पढ़ें
env('APP_DEBUG', false)
environment वेरिएबल पढ़ें
route('posts.show', $post)
नामित route को URL उत्पन्न करें
url('/dashboard')
पूर्णतः योग्य URL उत्पन्न करें
asset('css/app.css')
public asset हेतु URL
old('email')
पुराना flash किया input प्राप्त करें
now()->addDays(7)
वर्तमान Carbon timestamp
Str::slug('My Title')
String helper (slugify)
collect([1, 2, 3])->sum()
array से collection बनाएं
cache()->remember('key', 60, $fn)
एक अवधि हेतु मान कैश करें
dd($value)
मान dump करें और die करें
Storage::put('file.txt', $contents)
storage में फ़ाइल लिखें
कोई प्रविष्टि “:q” से मेल नहीं खाती।
मदद चाहिए?
इस टूल में कोई समस्या मिली? हमारी टीम को बताएं।