我有一个带有自定义验证规则的模型,在每个模型中我都有变量$ rules:
public static $rules = [...];
https://medium.com/@konafets/a-better-place-for-your-validation-rules-in-laravel-f5e3f5b7cc
我要在自定义请求中使用以下规则:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
class ModelRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Auth::check() ? true : false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return static::$rules;
}
}
我收到错误消息:
Access to undeclared static property: App\Http\Requests\ModelRequest::$rules
在控制器中:
public function store(ModelRequest $request)
{
...
}
这是全局的。我需要获取模型实例,但是如何?
答案 0 :(得分:0)
如果您在模型中放置了 $ rules 变量,则不能像这样访问它,因为 static 是指您当前所在的类。 试试看:
通知:我假设您的模型位于 App 名称空间
下
// In your model class
class YourModel extends Model{
const RULES=[];
}
//Then in your request class
class ModelRequest extends FormRequest
{
public function rules()
{
return App\YourModel::RULES;
}
}