Code feed
Code worth pausing your scroll for.
Filter your feed Active
Community signal
ShuffleWarn When a Return Value Is Ignored
Use #[NoDiscard] when callers should not accidentally ignore an important result.
Warn When a Return Value Is Ignored
#[\NoDiscard]
function saveOrder(Order $order): bool
{
return $order->save();
}
$saved = saveOrder($order);
Route Laravel Jobs Centrally
Define the default connection and queue for a job class in one place.
Route Laravel Jobs Centrally
use App\Jobs\ProcessPodcast;
use Illuminate\Support\Facades\Queue;
Queue::route(
ProcessPodcast::class,
connection: 'redis',
queue: 'podcasts',
);
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
Attach structured metadata to routes
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#add-route-metadata-support
Attach structured metadata to routes
<?php
Route::get('/pricing', PricingController::class)
->metadata([
'seo' => ['title' => 'Pricing', 'robots' => 'index'],
'feature' => 'public-pricing',
]);
$title = request()->route()->getMetadata('seo.title');
// Further reading: https://laravel.com/framework/docs/changelog#add-route-metadata-support
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
Hello, CodeSnap!
const greet = name => {
return `Hello, ${name}!`;
};
console.log(greet('developer'));
Hello, CodeSnap!
const greet = name => {
return `Hello, ${name}!`;
};
console.log(greet('developer'));
Get the First and Last Array Values
PHP 8.5 adds native functions that return null for empty arrays.
Get the First and Last Array Values
<?php
$firstUser = array_first($users);
$lastUser = array_last($users);
$firstUser ??= 'No users found';
Group mixed AND and OR conditions
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Group mixed AND and OR conditions
<?php
$people = Person::query()
->where(fn ($q) => $q
->where('age', '>=', 18)
->where('plan', 'adult'))
->orWhere(fn ($q) => $q
->where('age', '>=', 65)
->where('plan', 'senior'))
->get();
// Further reading: https://laravel-news.com/eloquent-tips-tricks
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