0

我有一个像这样的简单代码(更多或更少)。

路线:

Route::post('car', 'CarController@store')->name('insert_car.store');
Route::post('car-italian', 'CarItalianController@store')->name('insert_car-italian.store');

控制器:

class CarController extends Controller
{
    public function store(StoreCarRequest $request)
    {
        return Car::create($request->validated()) // Calling 'car-italian' route, the code fails here!
    }
}
class CarItalianController extends Controller
{
    public function store(StoreCarItalianRequest $request)
    {
        $input_parameters = $request->validated();
        $t = [
            'model' => $input_parameters['modello'],
            'door' => $input_parameters['porte'],
        ];
        return (new CarController)->store(new StoreCarRequest($t));
    }
}

表格要求:

class StoreCarRequest extends FormRequest
{
    public function rules()
    {
        return [
            'model' => 'string',
            'door'  => 'integer'
        ];
    }
}
class StoreCarItalianRequest extends FormRequest
{
    public function rules()
    {
        return [
            'modello' => 'string',
            'porte'   => 'integer'
        ];
    }
}

当我调用路由car-italian时,它失败并显示消息:

Call to a member function validated() on null

有人能帮我吗?我花了一整天的时间:-/

谢谢

4

1 回答 1

0

好的,我找到了更新控制器的解决方案CarItalianController

class CarController extends Controller
{
    public function store(StoreCarRequest $request)
    {
        return Car::create($request->validated()) 
    }
}
class CarItalianController extends Controller
{
    public function store(StoreCarItalianRequest $request)
    {
        $input_parameters = $request->validated();
        $t = [
            'model' => $input_parameters['modello'],
            'door' => $input_parameters['porte'],
        ];

        $insertRequest = new StoreCarRequest();
        $insertRequest->setValidator(Validator::make($t, $insertRequest->rules()));
        return (new CarController)->store($insertRequest);
    }
}
于 2021-11-12T11:25:28.043 回答