Laravel在请求验证中使用config()

时间:2019-05-30 15:59:15

标签: php laravel laravel-5

我正在使用laravel 5.8,我想在Request验证类中使用access global config(),但它不起作用

namespace App\Http\Requests;

use App\AppConstant;
use Illuminate\Foundation\Http\FormRequest;

class Something extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'url' => 'required',
            'category' => 'in:'.implode(",", config('app.categories').''
        ];
    }
}

这是我的config / app.php

的一部分
return [

    'name' => env('APP_NAME', 'Laravel'),
    'categories' => [
        'games',
        'entertainment'
    ],


但输出是

Class App\\Http\\Requests\\Something does not exist
when i remove config() from request file it works very well

1 个答案:

答案 0 :(得分:1)

您的代码在类别之后缺少右括号)。应该是

'category' => 'in:'.implode(",", config('app.categories')).''