Laravel Tips and Tricks: Hidden Features Most Developers Miss

Here's something interesting—Laravel comes with over 100 built-in Artisan commands, but most developers stick to using just 10 of them regularly.

Many of us barely scratch the surface of what Laravel can really do. We miss out on powerful features that could make our code better and easier to maintain. Our team has found many hidden gems over the years that have reshaped the way we work with Laravel.

This piece shares Laravel tips and tricks that fly under most developers' radar. You'll find everything from lesser-known Artisan commands to advanced service container techniques that can substantially improve your Laravel projects. These practical tips will help you write cleaner, more efficient code, whether you’re an experienced Laravel developer or just starting out.

Time to head over to Laravel's hidden treasures and realize its full potential.


Lesser-Known Artisan Commands

Custom Command Creation

php artisan make:command CheckUsers

Define arguments and options in your command class:

protected $signature = 'check:users {userId?} {--verified}';
protected $description = 'Get count of users with optional verification status';

Database Management Commands

  • php artisan db:show – Overview of all connections
  • php artisan db:table users – Inspect table structure
  • php artisan db:monitor – Watch connection health

Maintenance Mode Options

php artisan down --secret="12345" --render="errors::503" --retry=60

This enables maintenance mode with a bypass token, pre-rendered view, and auto-retry every 60 seconds. Queued jobs pause automatically.


Advanced Request Handling

Form Request Validation

php artisan make:request StoreUserRequest
class StoreUserRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users',
        ];
    }
}

Custom Validation Rules

Validator::extend('custom_rule', function ($attribute, $value, $parameters) {
    return $value === 'desired_value';
});

Request Lifecycle Optimization

Use middleware to preprocess requests:

public function handle($request, Closure $next)
{
    $request->merge([
        'processed_at' => now(),
        'normalized_input' => $this->normalizeInput($request->all()),
    ]);
    return $next($request);
}

Service Container Tips

Contextual Binding

$this->app->when(PaymentController::class)
    ->needs(PaymentService::class)
    ->give(function () {
        return new StripePaymentService();
    });

Custom Deferred Service Provider

class DeferredServiceProvider extends ServiceProvider
{
    protected $defer = true;

    public function provides()
    {
        return ['deferred.service'];
    }

    public function register()
    {
        $this->app->singleton('deferred.service', function ($app) {
            return new DeferredService();
        });
    }
}

Container Resolution Events

$this->app->resolving(function ($object, $app) {
    // Called on every resolution
});

$this->app->resolving(HelpSpot::class, function ($helpSpot, $app) {
    // Only for HelpSpot instances
});

Cache and Session Management

Redis Configuration

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

CACHE_DRIVER=redis
SESSION_DRIVER=redis

Cache Tags (Redis Only)

// Store with tags
Cache::tags(['people', 'developers'])->put('profile', $data, 600);

// Retrieve
$data = Cache::tags(['people'])->get('profile');

// Flush specific tag
Cache::tags(['developers'])->flush();

Note: Cache tags only work with Redis and Memcached.


Event Broadcasting Features

Broadcast-Ready Event

class OrderShipped implements ShouldBroadcast
{
    public function __construct(public Order $order) {}

    public function broadcastOn()
    {
        return new PrivateChannel('orders.' . $this->order->user_id);
    }
}

Laravel Echo Setup (Frontend)

window.Echo = new Echo({
    broadcaster: 'reverb',
    key: process.env.MIX_REVERB_APP_KEY,
    wsHost: window.location.hostname,
    wsPort: 6001,
    forceTLS: false,
});

Queue Strategies

  • Use ShouldBroadcastNow for instant delivery
  • Assign events to dedicated queues: return 'real-time';
  • Run multiple queue workers for different priorities

Conclusion

Laravel is a powerful framework with an extensive range of features, and there are many hidden gems that can greatly enhance your development experience. From lesser-known Artisan commands and advanced request handling techniques to mastering the service container and optimizing cache and session management, Laravel provides countless ways to write cleaner, more efficient code.

Implementing event broadcasting for real-time features and leveraging Redis for high-performance caching are just a few examples of how you can supercharge your Laravel projects.

By becoming familiar with these Laravel tips and tricks, you'll be able to build more scalable, maintainable, and efficient applications. Whether you're a beginner or a seasoned developer, exploring these advanced techniques will elevate your development process and help you make the most out of Laravel's powerful features. Stay curious, and keep exploring to unlock the full potential of Laravel!


Frequently Asked Questions

How do I create a custom Artisan command in Laravel?+
What’s the difference between ShouldBroadcast and ShouldBroadcastNow?+
Which cache drivers support cache tags in Laravel?+
How can I inspect a specific database table in Laravel?+
What is contextual binding in Laravel’s service container?+