---
name: laravel-96571edd41bb
description: Expert Laravel assistance covering Eloquent ORM, Blade templating, queues, service providers, and Artisan CLI. Use when developing enterprise PHP web applications and robust REST APIs.
---

# Laravel

Laravel is an expressive PHP web application framework providing an elegant developer experience with Eloquent ORM, robust queues, events, and native real-time broadcasting.

## When to Use

- **Enterprise Full-Stack PHP Web Applications**: Building robust web portals with Eloquent ORM, Blade, and queues.
- **Modern Monoliths with Inertia.js**: Pairing Laravel backend routes directly with React or Vue frontends without API boilerplate.
- **High-Velocity REST APIs**: Utilizing Laravel API Resources, Sanctum authentication, and Form Requests.
- **Real-Time Applications with Laravel Reverb**: First-party WebSocket broadcasting and real-time dashboard events.

## Quick Start

```php
// routes/web.php
Route::get('/', function () {
    return view('welcome');
});

// app/Models/User.php
// Elegant Active Record
$users = User::where('active', 1)->get();
```

## Core Concepts

### Eloquent ORM with Relationships & Scopes

Expressive database models with type hinting:

```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Builder;

class Customer extends Model
{
    protected $fillable = ['name', 'email', 'is_active'];

    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    // Local query scope
    public function scopeActive(Builder $query): void
    {
        $query->where('is_active', true);
    }
}
```

### Form Requests & Validated Controllers

Strict input validation separated from controller logic:

```php
namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreOrderRequest extends FormRequest
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    public function rules(): array
    {
        return [
            'customer_id' => 'required|exists:customers,id',
            'amount' => 'required|numeric|min:0.01',
            'items' => 'required|array|min:1',
            'items.*.product_id' => 'required|integer',
            'items.*.qty' => 'required|integer|min:1',
        ];
    }
}

// In Controller:
namespace App\Http\Controllers;

use App\Http\Requests\StoreOrderRequest;
use App\Models\Order;
use Illuminate\Http\JsonResponse;

class OrderController extends Controller
{
    public function store(StoreOrderRequest $request): JsonResponse
    {
        $validated = $request->validated();
        $order = Order::create($validated);

        return response()->json($order, 201);
    }
}
```

### Asynchronous Queue Workers & Jobs

Offloading heavy processing to Redis or SQS workers:

```php
namespace App\Jobs;

use App\Models\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class ProcessInvoiceJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(public Order $order) {}

    public function handle(): void
    {
        // Generate PDF and send invoice email
    }
}
```

## Common Patterns

### Eloquent Scope and Queue Dispatch

**Problem**: Processing heavy invoice calculations synchronously blocks user HTTP requests.

**Solution**:
Dispatch queued jobs with Eloquent local scopes:

```php
namespace App\Http\Controllers;

use App\Models\Order;
use App\Jobs\ProcessInvoiceJob;
use Illuminate\Http\JsonResponse;

class OrderController extends Controller
{
    public function complete(int $id): JsonResponse
    {
        $order = Order::active()->findOrFail($id);
        $order->update(['status' => 'completed']);

        // Dispatch to background queue worker
        ProcessInvoiceJob::dispatch($order)->onQueue('invoices');

        return response()->json(['message' => 'Order completed, invoice queued']);
    }
}
```

## Best Practices

**Do**:

- Target Laravel 11/12 with streamlined application structure and minimal configuration files.
- Use Form Requests for input validation instead of validating inline inside controllers.
- Utilize Eloquent eager loading (`with(['customer', 'items'])`) to prevent N+1 queries.
- Run queue workers under supervisor with Redis for reliable background job execution.

**Don't**:

- Execute raw database queries in Blade views or controllers; use Eloquent or repository classes.
- Run migrations directly in production without a verified backup and dry run.
- Commit the `.env` file to source control.

## Troubleshooting

| Error                                                 | Cause                                                       | Solution                                                              |
| :---------------------------------------------------- | :---------------------------------------------------------- | :-------------------------------------------------------------------- |
| `No application encryption key has been specified`    | `APP_KEY` missing in `.env` configuration file.             | Run `php artisan key:generate`.                                       |
| `Class '...' not found / Target class does not exist` | Namespace mismatch or service provider not loaded.          | Run `composer dump-autoload` and check namespace in `config/app.php`. |
| `SQLSTATE[HY000] [2002] Connection refused`           | Database service down or `.env` DB host/port misconfigured. | Verify database credentials and host (`127.0.0.1` vs `localhost`).    |

## References

- [Laravel Documentation](https://laravel.com/)


---
Source attribution: G1Joshi
Original project: https://github.com/G1Joshi/Agent-Skills/blob/6a74cf03a8a86dc7d993ead3091b41123dbd0181/skills/frameworks/laravel/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 G1Joshi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: G1Joshi
Original project: https://github.com/G1Joshi/Agent-Skills/blob/6a74cf03a8a86dc7d993ead3091b41123dbd0181/skills/frameworks/laravel/SKILL.md
Attribution basis: Source repository record

Fields of work: Development & Engineering
Roles: Backend Developer, DevOps / SRE, Full-Stack Developer
Specialties: Backend, Cross Platform
Tools: Laravel
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 1812d9c922db3b7509da3651ab46fabdbbfddb9463936c8c316a45faae2b0e4d
