Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleMigrate storage with a read-through disk
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/filesystem#scoped-read-only-and-read-through-filesystems
Migrate storage with a read-through disk
<?php
// config/filesystems.php
'assets' => [
'driver' => 'read-through',
'primary' => 'r2',
'fallback' => 'legacy-s3',
'throw_on_promotion_failure' => false,
],
// A fallback read is promoted to the primary disk.
$logo = Storage::disk('assets')->get('logos/app.svg');
// Further reading: https://laravel.com/docs/13.x/filesystem#scoped-read-only-and-read-through-filesystems
Generate a complete model set
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Generate a complete model set
<?php
// Model + migration + factory + seeder + controller.
// Run this command in your terminal:
php artisan make:model Company -mfsc
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Inspect HTTP fake request URIs
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#add-clientrequesturi
Inspect HTTP fake request URIs
<?php
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
Http::assertSent(fn (Request $request) =>
$request->uri()->path() === '/api/users'
&& $request->uri()->query()->integer('page') === 2
);
// Further reading: https://laravel.com/framework/docs/changelog#add-clientrequesturi
Extend the new Artisan dev runner
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#add-artisan-dev-command
Extend the new Artisan dev runner
<?php
use Illuminate\Foundation\Console\DevCommands;
DevCommands::artisan('reverb:start', 'reverb')->orange();
DevCommands::register(
'stripe listen --forward-to '.config('app.url'),
'stripe',
)->green();
// Start everything with: php artisan dev
// Further reading: https://laravel.com/framework/docs/changelog#add-artisan-dev-command
Debounce Queued Event Listeners
Process only the newest event when several events arrive during a short period.
Debounce Queued Event Listeners
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Attributes\DebounceFor;
#[DebounceFor(30, maxWait: 120)]
class UpdateProductSearchIndex implements ShouldQueue
{
public function debounceId(ProductUpdated $event): string
{
return (string) $event->product->getKey();
}
}
Find it or create it
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Find it or create it
<?php
$user = User::firstOrCreate(
['email' => $email],
['name' => $name],
);
$order = Order::findOrFail($orderId);
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Return a JSON:API resource
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/docs/13.x/eloquent-resources#json-api-resources
Return a JSON:API resource
<?php
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
final class PostResource extends JsonApiResource
{
public $attributes = ['title', 'body', 'created_at'];
public $relationships = ['author', 'comments'];
}
return $post->toResource();
// Further reading: https://laravel.com/docs/13.x/eloquent-resources#json-api-resources
Configure Read-Through File Storage
Read from an old disk and automatically copy missing files to the new primary disk.
Configure Read-Through File Storage
'assets' => [
'driver' => 'read-through',
'primary' => 'r2',
'fallback' => 'legacy-s3',
],
Clone a model safely
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Clone a model safely
<?php
$draft = $post->replicate(['slug', 'published_at']);
$draft->title = "Copy of {$post->title}";
$draft->save();
// Further reading: https://laravel-news.com/eloquent-tips-tricks
Fetch several IDs at once
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Fetch several IDs at once
<?php
$users = User::findMany([1, 4, 9]);
$emails = $users->pluck('email');
// Further reading: https://laravel-news.com/eloquent-tips-tricks