Code feed
Code worth pausing your scroll for.
Filter your feed
Community signal
ShuffleRead the affected row count
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Read the affected row count
<?php
$updated = Product::query()
->whereNull('category_id')
->update(['category_id' => $fallbackCategoryId]);
logger()->info("Updated {$updated} products");
// Further reading: https://laravel-news.com/eloquent-tips-tricks
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
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
Get the latest related model
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Get the latest related model
<?php
public function latestPost(): HasOne
{
return $this->hasOne(Post::class)
->latestOfMany();
}
// 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
Update counters atomically
Original CodeSnap example inspired by Laravel News. Source: https://laravel-news.com/eloquent-tips-tricks
Update counters atomically
<?php
$post = Post::findOrFail($postId);
$post->increment('views');
$post->increment('score', 5);
$post->decrement('stock');
// 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
Dispatch many jobs with Bus bulk
Laravel 13 API example based on official Laravel documentation. Source: https://laravel.com/framework/docs/changelog#introduce-busbulk
Dispatch many jobs with Bus bulk
<?php
use Illuminate\Support\Facades\Bus;
Bus::bulk(
$users
->map(fn (User $user) => new RefreshProfile($user))
->all(),
);
// Further reading: https://laravel.com/framework/docs/changelog#introduce-busbulk
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
Find the first matching array value
PHP 8.4 tip: use array_find() instead of a manual search loop. Source: php.net/releases/8.4
Find the first matching array value
<?php
$admin = array_find(
$users,
static fn (User $user): bool => $user->isAdmin(),
);
// $admin is the first match, or null.