deligoez / laravel-model-hashid Goto Github PK
View Code? Open in Web Editor NEW#️⃣ Generate, Save, and Route Stripe/Youtube-like Hash IDs for Laravel Eloquent Models
#️⃣ Generate, Save, and Route Stripe/Youtube-like Hash IDs for Laravel Eloquent Models
HashIdModelConfig
→ Config
When upgrading to version 2.4.0 my routes stopped working. I am using route model binding with ModelA and I have the following routes in web.php
Route::get('/permanent/modelA/{modelA}', function ($modelA) {
$modelA = ModelA::findByHashId($modelA);
if ($modelA) {
return redirect(route('modelA.show', ['modelA' => $modelA]));
}
abort(404);
})->name('modelA.permalink');
Route::get('/modelA/{modelA:slug}', [ModelAController::class, 'show'])->name('modelA.show');
The idea being that the slug might change but I would always be able to use the hash id to find the model. This behavior no longer works with version 2.4.0. The easiest work around is to use explicit model binding.
When trying to find a model using a hash id that doesn't exist, this exception is thrown:
Undefined array key 0
The function that is called can return the following values:
@return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null
Which is then accessed:
return $generator->decode($hashIdInstance->hashIdForKey)[0];
When a model can't be found, I suggest a null being returned instead.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.