Skip to content

Instantly share code, notes, and snippets.

View brentkelly's full-sized avatar

Brent Kelly brentkelly

View GitHub Profile
@brentkelly
brentkelly / laravel-custom-routing-force-asset-path.php
Last active March 27, 2024 11:01
Customise Laravel to support forcing the asset root at runtime
<?php
namespace App\Services;
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator;
class UrlGenerator extends BaseUrlGenerator
{
/**
* The original asset root so we can restore it
*/
@brentkelly
brentkelly / ModelSyncManager.md
Last active May 27, 2024 05:25
A Laravel service to replace an existing dataset with a new dataset, syncing new, updated, and deleted items.

Model Sync Manager

A Laravel service for replacing an existing dataset with a new dataset - updating, inserting & deleting records as efficiently as possible to transform the existing database state into the new desired state. This works for any adhoc set of records, or on a HasMany relationship.

A common scenario with saving or syncing data is the need to replace an existing set of records with an updated set - which may include new or removed items.

Laravel provides a sync method for many-to-many relationships which is a similar concept but only replaces the relationships - requiring you to know the IDs of the records you want to attach to, and not impacting the underlying data. It also only works on the relationships of a single parent at a time, whereas we may want to sync records for a large group of parents in one go.

Laravel also provides an upsert method which is a fast