Laravel Quick Revision Guide at current stage

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_resets table).
  • 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/create will 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 tinker

What you can do:

  • Test queries
  • Create records
  • Debug code

Useful shortcuts:

  • exit โ†’ quit tinker
  • Ctrl + 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 --resource

Creates 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 ๐Ÿš€