Laravel Cheatsheet in Details — All Commands with Definition (Advanced Topics Covered)
Agar aap Laravel seekh rahe ho ya already kaam kar rahe ho, to ek baat sabko pata hai — Laravel mein itne saare Artisan commands, Eloquent methods, aur helper functions hain ki yaad rakhna mushkil ho jaata hai. Isi wajah se hum yeh detailed cheatsheet laaye hain, jisme har cheez ko step by step, simple bhasha mein samjhaya gaya hai — jaise koi senior developer aapko side mein baitha kar samjha raha ho.
Is guide mein hum cover karenge: Artisan commands, routing, controllers, migrations, Eloquent ORM, Blade templating, middleware, validation, queues, events aur listeners, aur kuch advanced topics jaise service providers aur API resources. Chaliye shuru karte hain.
1. Laravel Installation aur Basic Commands
Sabse pehle, naya Laravel project banane ke liye:
composer create-project laravel/laravel project-name
Yeh command Composer ke through ek fresh Laravel installation banata hai. Agar Laravel installer already installed hai to:
laravel new project-name
Project ke andar jaake local server chalane ke liye:
php artisan serve
Definition: Yeh command ek built-in PHP development server start karta hai, jo default http://127.0.0.1:8000 par chalta hai. Production ke liye yeh use nahi hota, sirf local testing ke liye hai.
2. Artisan Commands — Poori List Definition Ke Saath
Artisan Laravel ka command-line interface hai. Iske through aap boilerplate code generate kar sakte ho, database manage kar sakte ho, aur bahut kuch.
Make Commands (Files Generate Karne Ke Liye)
php artisan make:controller UserController
Definition: Ek naya controller file banata hai app/Http/Controllers folder mein. Controller wo jagah hai jaha aap request handle karne ka logic likhte ho.
php artisan make:model Product
Definition: Ek Eloquent model file banata hai. Model database table ko represent karta hai — is case mein products table ko.
php artisan make:model Product -mcr
Definition: Yeh ek shortcut hai — model ke saath saath migration (-m), controller (-c), aur resource controller (-r) bhi ek saath bana deta hai. Time bachane wala command hai.
php artisan make:migration create_products_table
Definition: Migration file banata hai jisme aap database table ka structure define karte ho — columns, data types, constraints.
php artisan make:middleware CheckAge
Definition: Naya middleware banata hai. Middleware un requests ko filter karta hai jo application mein aati hain — jaise authentication check, role check, etc.
php artisan make:request StoreProductRequest
Definition: Form Request class banata hai, jisme aap validation rules alag se define kar sakte ho, controller ko clean rakhte hue.
php artisan make:seeder ProductSeeder
Definition: Seeder file banata hai jo database mein dummy ya default data insert karne ke kaam aata hai.
php artisan make:factory ProductFactory
Definition: Factory class banata hai jo testing ya seeding ke liye fake/random data generate karta hai — Faker library ke through.
php artisan make:event OrderShipped
php artisan make:listener SendShipmentNotification
Definition: Event wo cheez hai jo application mein "kuch hua" signal deta hai (jaise order shipped hua), aur listener us event ko sunkar action leta hai (jaise notification bhejna).
php artisan make:job ProcessPodcast
Definition: Job class banata hai jo queue mein daal kar background mein process kiya ja sakta hai — heavy tasks ke liye useful, jaise email bhejna ya image process karna.
php artisan make:command SendEmailsCommand
Definition: Custom Artisan command banane ke liye — aap apna khud ka php artisan command bana sakte ho.
php artisan make:policy ProductPolicy
Definition: Policy class banata hai jo authorization logic define karta hai — kaun kya kar sakta hai (edit, delete, view).
php artisan make:resource ProductResource
Definition: API Resource class banata hai, jo database data ko JSON response mein transform karne ke liye use hota hai — API banate waqt bahut kaam aata hai.
Database Commands
php artisan migrate
Definition: Sabhi pending migrations ko run karta hai aur database mein tables create/update karta hai.
php artisan migrate:rollback
Definition: Last batch ki migrations ko undo karta hai — agar galti se koi change kar diya to wapas la sakte ho.
php artisan migrate:refresh
Definition: Sabhi migrations ko rollback karke phir se run karta hai — database ko fresh state mein le aata hai.
php artisan migrate:fresh
Definition: Sabhi tables drop karke migrations dobara run karta hai. Rollback se alag hai — yeh history nahi use karta, seedha sab drop karta hai.
php artisan db:seed
Definition: Database mein seeders ke through data insert karta hai.
php artisan migrate:fresh --seed
Definition: Ek hi command mein database fresh karke seed bhi kar deta hai — development mein bahut use hota hai.
Cache aur Config Commands
php artisan config:cache
Definition: Sabhi config files ko ek single cached file mein combine kar deta hai — performance improve hoti hai production mein.
php artisan config:clear
Definition: Config cache ko clear karta hai. Agar .env change kiya hai aur effect nahi ho raha, to yeh command chalao.
php artisan cache:clear
Definition: Application cache clear karta hai (jo aapne Cache::put() se store kiya tha).
php artisan route:cache
Definition: Routes ko cache kar deta hai, jisse routing fast ho jaati hai. Note: closures wale routes cache nahi hote.
php artisan route:clear
Definition: Route cache clear karta hai.
php artisan view:clear
Definition: Compiled Blade views ka cache clear karta hai.
php artisan optimize
Definition: Config aur route dono ko cache karta hai ek saath — production deployment ke time useful.
php artisan optimize:clear
Definition: Sabhi cache (config, route, view, event) ek saath clear kar deta hai.
Other Useful Commands
php artisan route:list
Definition: Application ke sabhi registered routes ki list dikhata hai — method, URI, controller ke saath.
php artisan tinker
Definition: Ek interactive PHP shell open karta hai jisme aap application ke andar directly code run kar sakte ho — testing ke liye kaafi handy.
php artisan queue:work
Definition: Queue mein pending jobs ko process karna start karta hai.
php artisan queue:listen
Definition: Similar to queue:work, lekin har request pe naya process start karta hai — code changes turant reflect hote hain, development ke liye better.
php artisan storage:link
Definition: public/storage se storage/app/public tak ek symbolic link banata hai, taaki uploaded files publicly accessible ho.
php artisan key:generate
Definition: Application ke liye ek unique encryption key generate karta hai — .env file mein APP_KEY set hota hai.
3. Routing — Basics Se Advanced Tak
Routes routes/web.php ya routes/api.php mein define hote hain.
Route::get('/products', [ProductController::class, 'index']);
Route::post('/products', [ProductController::class, 'store']);
Route::put('/products/{id}', [ProductController::class, 'update']);
Route::delete('/products/{id}', [ProductController::class, 'destroy']);
Definition: Yeh HTTP methods (GET, POST, PUT, DELETE) ke basis par decide karte hain ki kaunsa controller method call hoga.
Resourceful routing ek shortcut hai jab aapko ek model ke liye standard CRUD routes chahiye:
Route::resource('products', ProductController::class);
Definition: Yeh ek hi line mein index, create, store, show, edit, update, destroy — saare 7 routes generate kar deta hai.
Route Groups aur Middleware
Route::middleware(['auth'])->group(function () {
Route::get('/dashboard', [DashboardController::class, 'index']);
});
Definition: Route group multiple routes ko common settings (jaise middleware, prefix) ke saath wrap karta hai — repetition kam hoti hai.
4. Eloquent ORM — Database Ke Saath Kaam Karna
Eloquent Laravel ka ORM (Object Relational Mapper) hai — matlab aap SQL query likhne ke bajaye PHP objects ke through database se baat karte ho.
$products = Product::all();
Definition: Table ke sabhi records fetch karta hai.
$product = Product::find(1);
Definition: Primary key ke basis par ek record fetch karta hai.
$products = Product::where('price', '>', 500)->get();
Definition: Condition ke basis par filtered records fetch karta hai.
Product::create(['name' => 'Laptop', 'price' => 45000]);
Definition: Naya record insert karta hai — mass assignment ke through, jiske liye model mein $fillable array define karna padta hai.
Eloquent Relationships
// One to Many
public function orders() {
return $this->hasMany(Order::class);
}
// Belongs To
public function user() {
return $this->belongsTo(User::class);
}
// Many to Many
public function roles() {
return $this->belongsToMany(Role::class);
}
Definition: Relationships batate hain ki ek model dusre model se kaise connected hai. hasMany ek-se-many relation, belongsTo reverse relation, aur belongsToMany pivot table ke through many-to-many relation define karta hai.
5. Blade Templating
@if($user->isAdmin())
Welcome Admin
@endif
@foreach($products as $product)
{{ $product->name }}
@endforeach
Definition: Blade Laravel ka templating engine hai. @if, @foreach jaise directives PHP logic ko HTML ke saath clean tarike se mix karne dete hain, bina tags likhe.
@extends('layouts.app')
@section('content')
Page Content
@endsection
Definition: Layout inheritance ka system hai — ek master layout banao, aur baaki pages usko extend karke apna content section define karein.
6. Middleware — Advanced Topic
Middleware requests ko application tak pahunchne se pehle ya response bhejne se pehle filter karta hai.
public function handle($request, Closure $next) {
if (!$request->user()->isAdmin()) {
return redirect('home');
}
return $next($request);
}
Definition: Is example mein middleware check kar raha hai ki user admin hai ya nahi — agar nahi, to redirect kar deta hai, warna request ko aage jaane deta hai.
7. Validation
$request->validate([
'name' => 'required|max:255',
'email' => 'required|email|unique:users',
]);
Definition: Incoming data ko rules ke against check karta hai. Agar validation fail hoti hai, to automatically error response ke saath user ko wapas bhej deta hai.
8. Queues aur Jobs — Advanced Topic
Jab koi task time-consuming ho (jaise bulk email bhejna), usse queue mein daal dete hain taaki user ko wait na karna pade.
dispatch(new ProcessPodcast($podcast));
Definition: Job ko queue mein daalta hai, jo background mein php artisan queue:work ke through process hota hai.
9. Events aur Listeners — Advanced Topic
event(new OrderShipped($order));
Definition: Event fire karta hai. Iske corresponding listener automatically trigger ho jaate hain, jo defined action perform karte hain (jaise notification bhejna).
10. API Resources — Advanced Topic
return ProductResource::collection(Product::all());
Definition: Database data ko structured JSON format mein transform karta hai, jo API response ke liye clean aur consistent hota hai.
11. Service Providers — Advanced Topic
Service Providers Laravel application ke bootstrapping process ka core hain — yahi jagah hai jaha services, bindings, aur event listeners register hote hain.
public function register() {
$this->app->bind(PaymentGateway::class, StripeGateway::class);
}
Definition: Yeh Service Container mein ek binding register karta hai — jab bhi PaymentGateway request kiya jayega, Laravel automatically StripeGateway ka instance de dega. Isse dependency injection flexible aur testable ban jaata hai.
Conclusion
Yeh tha ek complete Laravel cheatsheet — Artisan commands se leke advanced topics jaise service providers tak. Agar aap regularly Laravel mein kaam karte ho, to yeh reference bookmark kar lo — jab bhi koi command ya syntax bhool jao, seedha yaha wapas aa sakte ho.
Koi aur specific Laravel topic detail mein chahiye — jaise API authentication (Sanctum/Passport), testing (PHPUnit), ya deployment — to hume batao, hum us par bhi ek detailed guide layenge.
Kisi bhi query ke liye contact karo: admin@rupeshtechnologies.com