I have some route/model binding set up in my project for one of my models, and that works just fine. I'm able to use my binding in my route path and accept an instance of my model as a parameter to the relevant method in my controller.
现在我正在try 使用这个模型做一些工作,所以我在我的控制器中创建了一个方法,它接受表单请求,这样我就可以执行一些验证.
public function edit(EditBrandRequest $request, Brand $brand)
{
// ...
我的模型的每个不同实例都可以进行不同的验证,因此我需要能够使用模型的一个实例来构建一组自定义的验证规则.
Is there a way of getting the instance of the model, that is injected into the controller from the Form Request?个
我try 在表单请求的构造函数中输入模型实例的类型提示
class EditBrandRequest extends Request
{
public function __construct(Brand $brand)
{
dd($brand);
}
我还try 过在表单请求的rules()
方法中使用类型暗示模型实例.
class EditBrandRequest extends Request
{
// ...
public function rules(Brand $brand)
{
dd($brand);
In both instances I am provided an empty/new instance of the model, rather than the instance I am expecting.
Of course, I could always get around this by not bothering with Form Requests and just generate the rules in the controller and validate manually - but I would rather do it the Laravel way if it's possible.
谢谢