简体   繁体   English

Laravel验证从类请求添加输入请求时总是返回false

[英]Laravel validation always return false when tyring to add input request from class request

I've got the validation return false when i added some fields to the request (hardcoded). 当我向请求中添加一些字段(硬编码)时,验证返回false。

For more information you can see the file below (CreatePackageServiceRequest): 有关更多信息,请参见下面的文件(CreatePackageServiceRequest):

<?php

namespace App\Containers\Package\UI\API\Requests;

use App\Ship\Parents\Requests\Request;
use Auth;

/**
 * Class CreatePackageServiceRequest.
 */
class CreatePackageServiceRequest extends Request
{

    /**
     * The assigned Transporter for this Request
     *
     * @var string
     */
    // protected $transporter = \App\Ship\Transporters\DataTransporter::class;

    /**
     * Define which Roles and/or Permissions has access to this request.
     *
     * @var  array
     */
    protected $access = [
        'permissions' => '',
        'roles'       => '',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     *
     * @var  array
     */
    protected $decode = [
        // 'id',
    ];

    /**
     * Defining the URL parameters (e.g, `/user/{id}`) allows applying
     * validation rules on them and allows accessing them like request data.
     *
     * @var  array
     */
    protected $urlParameters = [
        // 'id',
    ];

    /**
     * @return  array
     */
    public function rules()
    {
        $this->request->set('user_id', Auth::user()->id);
        $this->request->set('vendor_id', Auth::user()->ownedVendor->id);
        // dd($this->all()); // we've got all the input we want here

        return [
            'name'                          => 'required|max:255',
            'has_limited_number_of_event'   => 'required',
            'min_length'                    => 'required|numeric',
            'max_length'                    => 'required|numeric|min:'.($this->get('min_length') + 1),
            'max_booking'                   => 'required',
            'message_on_booking_full'       => 'required',
            'allow_choose_host'             => 'required',
            'allow_skip'                    => 'required',
            'host_name'                     => 'required',
            'allow_extra_booking'           => 'required',
            'message_no_host_available'     => 'required',
            'has_photobooth_operator'       => 'required',
            'require_photobooth_background' => 'required',
            'require_customization'         => 'required',
            'customization_title'           => 'required',
            'customization_label'           => 'required',
            'user_id'                       => 'required',
            'vendor_id'                     => 'required',
            // '{user-input}' => 'required|max:255',
        ];
    }

    /**
     * @return  bool
     */
    public function authorize()
    {
        return $this->check([
            'hasAccess',
        ]);
    }
}

When i run dd($this->all()) , all input that i want is added in there. 当我运行dd($this->all()) ,我想要的所有输入都添加在那里。 But the result of this roles/validation show that two additional fields needs to be required. 但是,此角色/验证的结果表明,还需要两个附加字段。

Here is the response of validation, you can see the user_id, and vendor_id below. 这是验证的响应,您可以在下面看到user_id和vendor_id。

{
    "status": "error",
    "code": 0,
    "message": "The given data was invalid.",
    "errors": {
        "name": [
            "The name field is required."
        ],
        "has_limited_number_of_event": [
            "The has limited number of event field is required."
        ],
        "max_length": [
            "The max length must be at least 5."
        ],
        "max_booking": [
            "The max booking field is required."
        ],
        "message_on_booking_full": [
            "The message on booking full field is required."
        ],
        "allow_choose_host": [
            "The allow choose host field is required."
        ],
        "allow_skip": [
            "The allow skip field is required."
        ],
        "host_name": [
            "The host name field is required."
        ],
        "allow_extra_booking": [
            "The allow extra booking field is required."
        ],
        "message_no_host_available": [
            "The message no host available field is required."
        ],
        "has_photobooth_operator": [
            "The has photobooth operator field is required."
        ],
        "require_photobooth_background": [
            "The require photobooth background field is required."
        ],
        "require_customization": [
            "The require customization field is required."
        ],
        "customization_title": [
            "The customization title field is required."
        ],
        "customization_label": [
            "The customization label field is required."
        ],
        "user_id": [
            "The user id field is required."
        ],
        "vendor_id": [
            "The vendor id field is required."
        ]
    },
    "status_code": 422,
    "exception": "Illuminate\\Validation\\ValidationException",
    "file": "/iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php",
    "line": 117,
    "trace": "Illuminate\\Validation\\ValidationException: The given data was invalid. in /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/FormRequest.php:117\nStack trace:\n#0 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php(24): Illuminate\\Foundation\\Http\\FormRequest->failedValidation(Object(Illuminate\\Validation\\Validator))\n#1 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php(31): Illuminate\\Foundation\\Http\\FormRequest->validateResolved()\n#2 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Container/Container.php(1057): Illuminate\\Foundation\\Providers\\FormRequestServiceProvider->Illuminate\\Foundation\\Providers\\{closure}(Object(App\\Containers\\Package\\UI\\API\\Requests\\CreatePackageServiceRequest), Object(Illuminate\\Foundation\\Application))\n#3 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Container/Container.php(1021): Illuminate\\Container\\Container->fireCallbackArray(Object(App\\Containers\\Package\\UI\\API\\Requests\\CreatePackageServiceRequest), Array)\n#4 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Container/Container.php(1006): Illuminate\\Container\\Container->fireAfterResolvingCallbacks('App\\\\Containers\\\\...', Object(App\\Containers\\Package\\UI\\API\\Requests\\CreatePackageServiceRequest))\n#5 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Container/Container.php(665): Illuminate\\Container\\Container->fireResolvingCallbacks('App\\\\Containers\\\\...', Object(App\\Containers\\Package\\UI\\API\\Requests\\CreatePackageServiceRequest))\n#6 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Container/Container.php(601): Illuminate\\Container\\Container->resolve('App\\\\Containers\\\\...', Array)\n#7 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(740): Illuminate\\Container\\Container->make('App\\\\Containers\\\\...', Array)\n#8 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php(79): Illuminate\\Foundation\\Application->make('App\\\\Containers\\\\...')\n#9 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php(46): Illuminate\\Routing\\ControllerDispatcher->transformDependency(Object(ReflectionParameter), Array)\n#10 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/RouteDependencyResolverTrait.php(27): Illuminate\\Routing\\ControllerDispatcher->resolveMethodDependencies(Array, Object(ReflectionMethod))\n#11 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(41): Illuminate\\Routing\\ControllerDispatcher->resolveClassMethodDependencies(Array, Object(App\\Containers\\Package\\UI\\API\\Controllers\\Controller), 'createPackageSe...')\n#12 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\\Routing\\ControllerDispatcher->dispatch(Object(Illuminate\\Routing\\Route), Object(App\\Containers\\Package\\UI\\API\\Controllers\\Controller), 'createPackageSe...')\n#13 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\\Routing\\Route->runController()\n#14 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(659): Illuminate\\Routing\\Route->run()\n#15 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#16 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#17 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Auth\\Middleware\\Authenticate->handle(Object(Illuminate\\Http\\Request), Object(Closure), 'api')\n#18 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#19 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#20 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Routing\\Middleware\\ThrottleRequests->handle(Object(Illuminate\\Http\\Request), Object(Closure), 30, '1')\n#21 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#22 /iProject/Development/scotch.io/public/blackforest/app/Containers/Localization/Middlewares/LocalizationMiddleware.php(37): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#23 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): App\\Containers\\Localization\\Middlewares\\LocalizationMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#24 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#25 /iProject/Development/scotch.io/public/blackforest/app/Ship/Middlewares/Http/ProfilerMiddleware.php(26): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#26 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): App\\Ship\\Middlewares\\Http\\ProfilerMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#27 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#28 /iProject/Development/scotch.io/public/blackforest/app/Ship/Middlewares/Http/ProcessETagHeadersMiddleware.php(50): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#29 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): App\\Ship\\Middlewares\\Http\\ProcessETagHeadersMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#30 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#31 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#32 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#33 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#34 /iProject/Development/scotch.io/public/blackforest/app/Ship/Middlewares/Http/ValidateJsonContent.php(41): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#35 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): App\\Ship\\Middlewares\\Http\\ValidateJsonContent->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#36 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#37 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#38 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(661): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))\n#39 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(636): Illuminate\\Routing\\Router->runRouteWithinStack(Object(Illuminate\\Routing\\Route), Object(Illuminate\\Http\\Request))\n#40 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(602): Illuminate\\Routing\\Router->runRoute(Object(Illuminate\\Http\\Request), Object(Illuminate\\Routing\\Route))\n#41 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Router.php(591): Illuminate\\Routing\\Router->dispatchToRoute(Object(Illuminate\\Http\\Request))\n#42 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router->dispatch(Object(Illuminate\\Http\\Request))\n#43 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}(Object(Illuminate\\Http\\Request))\n#44 /iProject/Development/scotch.io/public/blackforest/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php(58): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#45 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Barryvdh\\Debugbar\\Middleware\\InjectDebugbar->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#46 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#47 /iProject/Development/scotch.io/public/blackforest/vendor/barryvdh/laravel-cors/src/HandleCors.php(36): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#48 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Barryvdh\\Cors\\HandleCors->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#49 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#50 /iProject/Development/scotch.io/public/blackforest/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#51 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Fideloper\\Proxy\\TrustProxies->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#52 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#53 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#54 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#55 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#56 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#57 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#58 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#59 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#60 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#61 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#62 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#63 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#64 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#65 /iProject/Development/scotch.io/public/blackforest/app/Containers/Debugger/Middlewares/RequestsMonitorMiddleware.php(27): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#66 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(149): App\\Containers\\Debugger\\Middlewares\\RequestsMonitorMiddleware->handle(Object(Illuminate\\Http\\Request), Object(Closure))\n#67 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Http\\Request))\n#68 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\{closure}(Object(Illuminate\\Http\\Request))\n#69 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))\n#70 /iProject/Development/scotch.io/public/blackforest/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter(Object(Illuminate\\Http\\Request))\n#71 /iProject/Development/scotch.io/public/blackforest/public/index.php(53): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))\n#72 /Users/macbookpro/.composer/vendor/laravel/valet/server.php(151): require('/iProject/Devel...')\n#73 {main}"
}

base on vivek_23 answer and after surfing on the internet, i found the solution for this issue. 基于vivek_23的答案,并在互联网上冲浪后,我找到了解决此问题的方法。

we can't add the additional fields on the rules method, we need to add them in constructor. 我们无法在rules方法上添加其他字段,我们需要在构造函数中添加它们。

here is my final code. 这是我的最终代码。

<?php

namespace App\Containers\Package\UI\API\Requests;

use App\Ship\Parents\Requests\Request;
use Auth;

/**
 * Class CreatePackageServiceRequest.
 */
class CreatePackageServiceRequest extends Request
{

    /**
     * The assigned Transporter for this Request
     *
     * @var string
     */
    // protected $transporter = \App\Ship\Transporters\DataTransporter::class;

    /**
     * Define which Roles and/or Permissions has access to this request.
     *
     * @var  array
     */
    protected $access = [
        'permissions' => '',
        'roles'       => '',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     *
     * @var  array
     */
    protected $decode = [
        // 'id',
    ];

    /**
     * Defining the URL parameters (e.g, `/user/{id}`) allows applying
     * validation rules on them and allows accessing them like request data.
     *
     * @var  array
     */
    protected $urlParameters = [
        // 'id',
    ];

    public function __construct(\Illuminate\Http\Request $request)
    {
        $request->request->add([
            'user_id' => Auth::user()->id, 
            'vendor_id' => Auth::user()->ownedVendor->id
        ]);
    }

    /**
     * @return  array
     */
    public function rules()
    {
        // $this->merge([
        //     'user_id' => Auth::user()->id, 
        //     'vendor_id' => Auth::user()->ownedVendor->id
        // ]);

        // dd($this->all());

        return [
            'name'                          => 'required|max:255',
            'has_limited_number_of_event'   => 'required',
            'min_length'                    => 'required|numeric',
            'max_length'                    => 'required|numeric|min:'.($this->get('min_length') + 1),
            'max_booking'                   => 'required',
            'message_on_booking_full'       => 'required',
            'allow_choose_host'             => 'required',
            'allow_skip'                    => 'required',
            'host_name'                     => 'required',
            'allow_extra_booking'           => 'required',
            'message_no_host_available'     => 'required',
            'has_photobooth_operator'       => 'required',
            'require_photobooth_background' => 'required',
            'require_customization'         => 'required',
            'customization_title'           => 'required',
            'customization_label'           => 'required',
            'user_id'                       => 'required',
            'vendor_id'                     => 'required',
            // '{user-input}' => 'required|max:255',
        ];
    }

    /**
     * @return  bool
     */
    public function authorize()
    {
        return $this->check([
            'hasAccess',
        ]);
    }
}

thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM