Replies: 4 comments
-
There's a method called |
Beta Was this translation helpful? Give feedback.
-
I was going to propose the same as @francoism90, but I realised it kinda depends on what you'd like to achieve (and where). The So, you can either do something like $data = SomeData::validateAndCreate($request->all()); or, as @francoism90 proposed, the class SomeData extends Data
{
public function __construct(
// properties
) {}
public static function fromRequest(SomeRequest $request): static
{
return self::validateAndCreate($request->all());
}
} The thing with All you need to do is pass the data through the I believe the problem could be if you have different rules in Unrelated to data validation, but posting this from the Data documentation, just in case it could be of use. |
Beta Was this translation helpful? Give feedback.
-
I am using a solution like this: <?php declare(strict_types=1);
namespace App\Concerns;
use Illuminate\Http\Request;
trait WithRouteParameters
{
public static function from(mixed ...$payloads): static
{
if ($payloads[0] instanceof Request) {
$request = $payloads[0];
$payloads = static::validate(
array_merge($request->all(), $request->route()?->parameters())
);
}
return parent::from($payloads);
}
} |
Beta Was this translation helpful? Give feedback.
-
I think @aidan-casey was working on an attribute which would make it possible to inject route parameters as data properties. Then regular validation would work just like you would expect. |
Beta Was this translation helpful? Give feedback.
-
In Laravel, we can do this trick to be able to validate route parameters during request.
So is there any way to validate route parameters with form data at the same time?
Beta Was this translation helpful? Give feedback.
All reactions