laravel/framework
Laravel is a modern PHP web application framework with expressive syntax and batteries-included tooling: fast routing, dependency injection, sessions/cache, migrations, queues, and real-time broadcasting—built to make development enjoyable for apps of any size.
## Getting Started
### Minimal Steps
1. **Installation**:
```bash
composer create-project laravel/laravel project-name
php artisan serve to spin up a local server for immediate testing.First Use Case:
routes/web.php:
Route::get('/greet', function () {
return "Hello, Laravel!";
});
php artisan make:controller UserController --resource
php artisan make:migration create_users_table
Define schema in the migration file, then run:
php artisan migrate
Where to Look First:
app/ Directory: Core application logic (controllers, models, providers).routes/ Directory: Web and API route definitions.config/ Directory: Framework configurations (e.g., app.php, database.php).resources/views/: Blade templates for frontend rendering.tests/: Built-in testing utilities (PHPUnit, Pest).Dependency Injection (DI) and Service Container:
AppServiceProvider:
public function register()
{
$this->app->bind(
\App\Contracts\PaymentGateway::class,
\App\Services\StripePayment::class
);
}
public function __construct(private PaymentGateway $paymentGateway) {}
Eloquent ORM:
app/Models/ with relationships:
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}
$posts = Post::with('comments')->where('published', true)->get();
Post::with(['comments', 'author'])->find(1);
Middleware:
php artisan make:middleware EnsureVerifiedEmail
app/Http/Kernel.php:
protected $routeMiddleware = [
'verified.email' => \App\Http\Middleware\EnsureVerifiedEmail::class,
];
Route::get('/dashboard', function () {
// ...
})->middleware('verified.email');
Artisan Commands:
Illuminate\Console\Command:
class SendEmailsCommand extends Command
{
protected $signature = 'emails:send';
protected $description = 'Send scheduled emails';
public function handle()
{
// Logic here
}
}
app/Console/Kernel.php and run:
php artisan emails:send
Events and Listeners:
event(new OrderPlaced($order));
EventServiceProvider:
protected $listen = [
OrderPlaced::class => [
SendOrderConfirmation::class,
],
];
API Resources:
class PostResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'comments' => CommentResource::collection($this->comments),
];
}
}
return new PostResource($post);
Queues and Jobs:
SendEmailJob::dispatch($user);
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue;
public function handle()
{
// Send email logic
}
}
php artisan queue:work
Testing:
public function test_example()
{
$response = $this->get('/');
$response->assertStatus(200);
}
Mockery or createMock:
$this->mock(PaymentGateway::class, function ($mock) {
$mock->shouldReceive('charge')->once();
});
config/app.php under providers.Blade::directive('datetime', function ($expression) {
return "<?php echo ($expression)->format('Y-m-d H:i:s'); ?>";
});
Str::macro('slug', function ($string) {
return Str::of($string)->slug();
});
php artisan vendor:publish --provider="Vendor\Package\ServiceProvider"
RefreshDatabase trait for database testing:
use RefreshDatabase;
public function test_user_creation()
{
$user = User::factory()->create();
// ...
}
N+1 Query Problem:
with() or load():
$posts = Post::with('comments')->get();
Session/Redis Connection Leaks:
withoutEvents() where possible.Attribute Parsing Conflicts:
#[WithoutRelations].Queue Job Timeouts:
php artisan queue:failed and retry:
php artisan queue:retry all
Route Caching:
php artisan route:clear
Validation Strict Mode:
flushState():
$request->flushState();
Carbon Timezone Issues:
Carbon::setTimezone('UTC');
Middleware Order:
app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\CheckForMaintenanceMode::class,
];
Eloquent Mutators/Accessors:
$attributes array for direct assignments:
$user->setRawAttributes(['name' => 'John']);
Queue Job Serialization:
#[WithoutRelations] or implement Serializable:
#[WithoutRelations]
class ComplexJob implements ShouldQueue {}
Log Everything:
Log::debug(), Log::info(), or dd() (dump and die) for debugging.config/logging.php.Tinker:
php artisan tinker for interactive debugging:
php artisan tinker
>>> $user = App\Models\User::first();
>>> $user->posts
Exception Handling:
How can I help you explore Laravel packages today?