If youโve been learning Laravel recently, this post will help you quickly revise some important concepts in a simple and practical way.
1. How Password Reset Works with Token
Laravel uses a secure token-based system for password resets:
- When a user requests a reset, Laravel generates a unique token.
- This token is stored in the database (usually in
password_resetstable). - A reset link with the token is sent via email.
- When the user clicks the link, Laravel verifies the token.
- If valid, the user can set a new password.
๐ Think of the token as a temporary secure key that proves the request is valid.
2. Custom Factory State (Dummy Data)
Factories help generate fake data for testing.
- A state lets you define a specific variation of data.
Example:
User::factory()->state(['role' => 'admin'])->create();You can also define named states inside the factory:
public function admin()
{
return $this->state([
'role' => 'admin',
]);
}๐ Useful when you need different types of test users (admin, customer, etc.).
3. Laravel Fallback Route
A fallback route runs when no other route matches.
Route::fallback(function () {
return "Page not found";
});๐ It acts like a custom 404 handler.
4. Why Route Order Matters
Laravel checks routes from top to bottom.
Example:
Route::get('/user/{id}', ...);
Route::get('/user/create', ...);Problem:
/user/createwill be treated as{id}
Solution:
- Always define specific routes first, then dynamic ones.
๐ Rule: Top = priority
5. Route Model Binding
Automatically fetch model data from route parameters.
Example:
Route::get('/users/{user}', function (User $user) {
return $user;
});Laravel:
- Finds user by ID
- Injects it into the route
๐ Saves time and avoids manual queries like User::find().
6. Model Relationships
Laravel makes it easy to connect tables.
Common types:
- One to One โ User โ Profile
- One to Many โ Post โ Comments
- Many to Many โ Users โ Roles
Example:
public function posts()
{
return $this->hasMany(Post::class);
}๐ Helps you work with related data easily using Eloquent.
7. Query Scopes
Scopes are reusable query filters.
Local Scope:
public function scopeActive($query)
{
return $query->where('status', 'active');
}Usage:
User::active()->get();๐ Keeps your queries clean and reusable.
8. Tinker (Laravel Playground)
Tinker lets you interact with your app in the terminal.
Run:
php artisan tinkerWhat you can do:
- Test queries
- Create records
- Debug code
Useful shortcuts:
exitโ quit tinkerCtrl + Cโ cancel current command- Arrow โ โ โ previous commands
๐ Think of Tinker as a live testing sandbox.
9. Resource Controller vs Normal Controller
Resource Controller:
php artisan make:controller PostController --resourceCreates methods like:
- index()
- create()
- store()
- show()
- edit()
- update()
- destroy()
Normal Controller:
- You define methods manually
๐ Resource controllers follow CRUD structure automatically.
10. Blade Naming Convention & โDotโ Syntax
Blade uses dot notation to map folders.
Example:
return view('admin.users.index');This maps to:
resources/views/admin/users/index.blade.php๐ Dot (.) = folder separator
๐ง 11. Laravel Caching (Speed Booster)
Caching means storing frequently used data so Laravel doesnโt have to fetch it again and again from the database.
โ Why use cache?
- Faster performance
- Reduced database load
๐น Common Cache Methods
1. Cache::put() (Store data)
use Illuminate\Support\Facades\Cache;
Cache::put('user_name', 'John', 60); // store for 60 seconds
2. Cache::get() (Retrieve data)
$name = Cache::get('user_name');
3. Cache::remember() (Best one โญ)
Stores data if not exists, otherwise returns cached data.
$users = Cache::remember('users', 60, function () {
return DB::table('users')->get();
});
๐ Very useful for DB queries.
4. Cache::forget() (Delete cache)
Cache::forget('users');
5. Cache::flush() (Clear all cache)
Cache::flush();
โ When NOT to use cache
- Frequently changing data (like live stock prices)
- User-specific sensitive data
- Small/simple queries (not worth caching)
๐ Clear cache when DB updates
Example:
public function update(Request $request, $id)
{
DB::table('users')->where('id', $id)->update([
'name' => $request->name
]);
Cache::forget('users'); // clear old cached data
}
๐งญ 12. Nested Routes (Clean URL Structure)
Nested routes help represent relationships.
๐งฉ Example: Posts & Comments
Route::resource('posts.comments', CommentController::class);
URL will look like:
/posts/1/comments/5
๐ Meaning: Comment 5 belongs to Post 1
โ Why useful?
- Clean structure
- Easy relationship handling
- Better readability
๐ 13. Form Request Validation (Clean Validation)
Instead of writing validation inside controllers, Laravel lets you move it into a separate class.
๐น Create Form Request
php artisan make:request StoreUserRequest
๐น Add validation
public function rules()
{
return [
'name' => 'required|max:255',
'email' => 'required|email|unique:users',
];
}
๐น Use in Controller
public function store(StoreUserRequest $request)
{
User::create($request->validated());
}
๐ Cleaner controller, reusable validation!
๐งฉ 14. Laravel Components (Reusable UI Blocks)
Components help you reuse UI parts like buttons, cards, layouts.
๐น Create Component
php artisan make:component Alert
๐น Blade file:
<div class="alert alert-{{ $type }}">
{{ $slot }}
</div>
๐น Use it:
<x-alert type="success">
Data saved successfully!
</x-alert>
โ Why useful?
- Reusable UI
- Cleaner Blade files
- DRY (Donโt Repeat Yourself)
๐จ 15. Why npm run dev fixes CSS?
When your CSS isnโt working, running:
npm run dev
๐ It compiles assets (CSS, JS) using Vite.
๐ก Whatโs happening?
- Laravel uses modern tools (Vite)
- Your CSS (especially Tailwind) is processed
- Output is generated in
/public/build
๐ง Without it:
Changes in CSS wonโt reflect.
โ๏ธ 16. Understanding vite.config.js
This file controls how your frontend assets are built.
๐น Example:
import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';
export default defineConfig({
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.js'],
refresh: true,
}),
],
});
๐งฉ What it does:
- Defines entry files (CSS & JS)
- Enables hot reload (auto refresh)
- Connects Laravel with Vite
๐ฅ Key benefits:
- Super fast builds
- Live reload
- Modern frontend workflow
Final Thoughts
These concepts are the building blocks of Laravel development:
- Routing controls flow
- Models manage data
- Blade handles UI
- Factories & Tinker help testing
- Faster (Caching)
- Cleaner (Form Requests & Components)
- Organized (Nested Routes)
- Modern (Vite & npm)
If you understand these well, you’re already on a solid path ๐
