Comments (5)
Thanks for the input!
That's also an interesting approach. Looks cleaner than newing up the rule manually.
I've added the static constructor method model()
. 👍
RouteKeyExists::model(Article::class)->replace()
from laravel-optimus.
Hello, @ivanvermeyen.
Package you provided seems useful. Good to have it as separate package as you've done it.
So, it's validating if model exists using encoded key and overwriting this id in request with decoded one?
If I will execute:
$validated = validate([
'article' => new RouteKeyExists(Article::class)->replace(),
]);
This will have actual Article id?
dd($validated['article']);
So much magic :}
from laravel-optimus.
Indeed, but you'll need extra parentheses:
$validated = validate([
'article' => (new RouteKeyExists(Article::class))->replace(),
]);
I was in doubt if I should add an optional second constructor parameter to enable replace
or add
. I think it's slightly less clear, but maybe prettier to look at without those extra parentheses? I think I will add this for flexibility...
$validated = validate([
'article' => new RouteKeyExists(Article::class, 'article'), // store the original ID in request('article')
]);
The real logic for checking if it exists is in the models resolveRouteBinding()
method, like the one in your trait, so it's not tightly coupled to a single use case. :) All I'm doing is this in essence:
if ( ! $model = $this->model->resolveRouteBinding($value)) {
return false; // validation fails...
}
And then some checks if I should replace or add the original ID to the request.
from laravel-optimus.
One more possible solution is static factory:
$validated = validate([
'article' => RouteKeyExists::model(Article::class)->replace(),
]);
Or name it as you like: make
or anything else. Or even this:
$validated = validate([
'article' => RouteKeyExists::replace(Article::class),
]);
from laravel-optimus.
Yeah, this one looks good. We could add suggestion for Laravel Optimus users to readme or composer.json that there is optional way to validate if encoded models are exists. But first of all I need to start using them myself. I've never done it in any production projects.
from laravel-optimus.
Related Issues (13)
- Return encoded route key in JSON output when using OptimusEncodedRouteKey HOT 7
- update to Laravel 5.6 HOT 3
- Update base Optimus package to v1.0
- Add collection macros for encoding and decoding HOT 1
- [Concept] Add helper trait for Eloquent models
- [Concept] Add scope trait for Eloquent models
- softdeletes resolveRouteBinding overwrite HOT 2
- How to use laravel-optimus encoded paths without using a closure in your route? HOT 8
- Update to Laravel 10 HOT 1
- It returns 0 when I encode or decode HOT 1
- Route Key Trait to use Optimus in models HOT 12
- Why not contributed to Laravel-Fakeid? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-optimus.