php
1️⃣ Route::get('/', fn() => 'Hello Laravel!');
2️⃣ Route::get('/user/{name}', fn($name) => "Hello, $name");
3️⃣ Route::get('/about', [PageController::class, 'about']);
4️⃣ Route::post('/contact', [ContactController::class, 'store']);
5️⃣ Route::view('/welcome', 'welcome');
6️⃣ Route::redirect('/old', '/new');
7️⃣ Route::get('/optional/{name?}', fn($name = 'Guest') => "Hi, $name");
8️⃣ Route::match(['get','post'],'/form', fn()=> 'Form handled');
9️⃣ Route::any('/hook', fn()=> 'Any method works');
🔟 Route::fallback(fn()=> '404 Not Found');
php
1️⃣ php artisan make:controller UserController
2️⃣ class UserController extends Controller {
public function index() { return view('users.index'); }
}
3️⃣ Route::resource('users', UserController::class);
4️⃣ Route::controller(ProductController::class)->group(function () {
Route::get('/products', 'index');
Route::post('/products', 'store');
});
5️⃣ Route::get('/home', [HomeController::class, 'index'])->name('home');
6️⃣ public function show(User $user) { return $user; }
7️⃣ public function store(Request $r) { User::create($r->all()); }
8️⃣ public function destroy(User $user) { $user->delete(); }
9️⃣ public function update(Request $r, User $u) { $u->update($r->all()); }
0️⃣ return redirect()->route('home')->with('success','Saved!');
php
1️⃣ php artisan make:model Post -m
2️⃣ $posts = Post::all();
3️⃣ $post = Post::find(1);
4️⃣ Post::create(['title'=>'Hello','body'=>'Laravel']);
5️⃣ $post->update(['title'=>'Updated']);
6️⃣ $post->delete();
7️⃣ Post::where('status','published')->get();
8️⃣ Post::orderBy('created_at','desc')->take(5)->get();
9️⃣ $user->posts; // relationship
0️⃣ $post->user->name;
1️⃣ $posts = Post::with('user')->get();
2️⃣ Post::findOrFail(5);
3️⃣ Post::whereBetween('id',[1,10])->get();
4️⃣ Post::pluck('title');
5️⃣ Post::count();
6️⃣ Post::exists();
7️⃣ Post::firstOrCreate(['title'=>'Intro']);
8️⃣ $post->increment('views');
9️⃣ $post->decrement('likes');
0️⃣ $post->comments()->create(['body'=>'Nice!']);
php
1️⃣ php artisan make:migration create_posts_table
2️⃣ Schema::create('posts', function (Blueprint $t) {
$t->id(); $t->string('title'); $t->timestamps();
});
3️⃣ php artisan migrate
4️⃣ php artisan migrate:rollback
5️⃣ DB::table('users')->insert(['name'=>'John']);
6️⃣ DB::table('users')->get();
7️⃣ DB::table('users')->where('id',1)->update(['name'=>'Mike']);
8️⃣ DB::table('users')->where('id',2)->delete();
9️⃣ DB::transaction(function() {
// your queries
});
0️⃣ DB::select('select * from users where id=?',[1]);
bash
1️⃣ {{ $name }}
2️⃣ {!! $html !!}
3️⃣ @if($age > 18) Adult @else Minor @endif
4️⃣ @foreach($users as $user) {{ $user->name }} @endforeach
5️⃣ @include('partials.header')
6️⃣ @extends('layouts.app')
7️⃣ @section('contentt' ) Welcome @endsection
8️⃣ @csrf
9️⃣ < form method="POST">@method('PUT') </ form>
0️⃣ {{ route('home') }}
php
1️⃣ $validated = $request->validate(['email'=>'required|email']);
2️⃣ php artisan make:request StoreUserRequest
3️⃣ public function rules() { return ['name'=>'required']; }
4️⃣ if($errors->any()) echo $errors->first();
5️⃣ $request->only(['name','email']);
6️⃣ $request->except(['_token']);
7️⃣ $request->has('email');
8️⃣ old('email');
9️⃣ session('success');
0️⃣ return back()->withErrors(['email'=>'Invalid']);
php
1️⃣ php artisan make:auth // (for Laravel 6–7)
2️⃣ php artisan ui bootstrap --auth
4️⃣ Auth::user();
5️⃣ Auth::id();
6️⃣ Auth::attempt(['email'=>$e,'password'=>$p]);
7️⃣ Auth::logout();
8️⃣ Route::middleware('auth')->get('/dashboard', fn()=>view('dash'));
9️⃣ @auth Welcome {{ auth()->user()->name }} @endauth
0️⃣ @guest Please login @endguest
php
1️⃣ $path = $request->file('avatar')->store('avatars');
2️⃣ Storage::disk('public')->put('file.txt','Hello');
3️⃣ Storage::get('file.txt');
4️⃣ Storage::delete('avatars/old.png');
5️⃣ Storage::url('avatars/me.png');
6️⃣ php artisan storage:link
7️⃣ < input type="file" name="avatar">
8️⃣ return response()->download($path);
9️⃣ File::exists($path);
0️⃣ File::delete($path);
php
1️⃣ Route::get('/api/users', fn()=> User::all());
2️⃣ return response()->json(['success'=>true]);
3️⃣ return response(User::find(1), 200);
4️⃣ return response()->json(['error'=>'Not Found'],404);
5️⃣ Route::apiResource('posts', PostController::class);
php
6️⃣ Artisan::call('cache:clear');
7️⃣ event(new NewUserRegistered($user));
8️⃣ Log::info('Something happened!');
9️⃣ Mail::to($user)->send(new WelcomeMail($user));
0️ Carbon::now()->addDays(5)->toDateString();
Copy snippets into small demo apps.
Try one concept per day.
Combine related snippets to make mini-projects.
Use php artisan tinker to test Eloquent snippets interactively.