Problem with PUT Laravel

Asked

Viewed 340 times

0

I’m using Laravel 5.6, routes and controller with Resource and in scritpt Post the code works normally, but with PUT it always gives error.

Follow the working code of the POST

        const fileInput = document.querySelector( '#trv_arquivo' );
        const formData = new FormData();
        if (document.querySelector('#trv_arquivo').value ){
            formData.set( 'trv_arquivo', fileInput.files[0] );
        }            
        formData.set( 'placa', document.querySelector('#placa').value );
        formData.set( 'marca_id', document.querySelector('#marca_id').value );
        formData.set( 'modelo_id', document.querySelector('#modelo_id').value );
        formData.set( 'cor_id', document.querySelector('#cor_id').value );
        formData.set( 'situacao_id', document.querySelector('#situacao_id').value );
        formData.set( 'pendencia_id', document.querySelector('#pendencia_id').value );
        formData.set( 'data_entrada', document.querySelector('#data_entrada').value );
        formData.set( 'data_saida', document.querySelector('#data_saida').value );
        formData.set( 'plaqueta', document.querySelector('#plaqueta').value );
        formData.set( 'trv', document.querySelector('#trv').value );
        formData.set( 'chave', document.querySelector('#chave').value );


        axios({
            method: 'post',
            url: '/automoveis',
            data: formData,
            config: { headers: {'Content-Type': 'multipart/form-data' }}
        })

No put I change the method to 'put', but always the following error in the console:

PUT http://projeto/automoveis 405 (Method Not Allowed)

Controller:

public function store(Request $request)
{

    $input = $request->all();

    request()->validate([
        'placa'         => 'required|max:10',
        'marca_id'      => 'required|max:3',
        'modelo_id'     => 'required|max:3',
        'cor_id'        => 'required|max:3',
        'situacao_id'   => 'required|max:3',
        'pendencia_id'  => 'required|max:3',
        'data_entrada'  => 'required|max:10',
        'data_saida'    => 'max:10',
        'plaqueta'      => 'max:11',
        'trv'           => 'max:11',
        'trv_arquivo'   => 'max:1024',
        'chave'         => 'max:3',
    ]);


    if (isset($input['trv_arquivo'])){

        request()->validate([
            'trv'           => 'required|max:11',
            ]);

        $input['trv_arquivo'] = "transito/automoveis/TRV-".$input['trv'];

        $trv_arquivo = $request->file('trv_arquivo');

        $trv_nome = "TRV-".$input['trv'].".pdf";        

        $path = $trv_arquivo->storeAs('transito/automoveis/', $trv_nome);
    }    



    $automovel = TransitoAutomovel::create($input);

    \Session::flash('success_message',trans('global.flash.fields.adicionar')); //<--FLASH MESSAGE

    return ['redirect' => route('automoveis.index')];

}

UPDATE

public function update(Request $request, $id)
    {
        $input = $request->all();

        request()->validate([
            'placa'         => 'required|max:10',
            'marca_id'      => 'required|max:3',
            'modelo_id'     => 'required|max:3',
            'cor_id'        => 'required|max:3',
            'situacao_id'   => 'required|max:3',
            'pendencia_id'  => 'required|max:3',
            'data_entrada'  => 'required|max:10',
            'data_saida'    => 'max:10',
            'plaqueta'      => 'max:11',
            'trv'           => 'max:11',
            'trv_arquivo'   => 'max:1024',
            'chave'         => 'max:3',
        ]);

        if (isset($input['trv_arquivo'])){

            request()->validate([
                'trv'           => 'required|max:11',
                ]);

            $input['trv_arquivo'] = "transito/automoveis/TRV-".$input['trv'];

            $trv_arquivo = $request->file('trv_arquivo');

            $trv_nome = "TRV-".$input['trv'].".pdf";        

            $path = $trv_arquivo->storeAs('transito/automoveis/', $trv_nome);
        }   

        $automovel = TransitoAutomovel::find($id);

        $automovel->update($input);

        \Session::flash('info_message',trans('global.flash.fields.editar')); // FLASH MESSAGE

        return ['redirect' => route('automoveis.index')];
    }
  • Merely informs that the method PUT is not allowed, you set this route as PUT using Route::put('automoveis') ?

  • Route::Resource('automoveis','Transito Transitoautomovelcontroller');

1 answer

1

When you use Route::resource('automoveis') to generate its routes, it generates the following routes:

Método        Caminho                       Ação    Route Name
GET           /automoveis                   index   automoveis.index 
GET           /automoveis/create            create  automoveis.create
POST          /automoveis                   store   automoveis.store
GET           /automoveis/{automovel}       show    automoveis.show
GET           /automoveis/{automovel}/edit  edit    automoveis.edit
PUT|PATCH     /automoveis/{automovel}       update  automoveis.update
DELETE        /automoveis/{automovel}       destroy automoveis.destroy

Therefore, the route POST works to register a Automovel but if Voce wants to update the information from Automovel, then you need to access the route /automoveis/{automovel}.

When you want to know the routes of your Laravel and what methods it allows, use php artisan route:list and Laravel himself will generate a list like the table I showed.

  • I looked here, actually it became route /automoveis/{automovei} (English reduction). But even putting this url /automoveis/{automovei} in put it generates console error: PUT http://project/automoveis/%7Bautomovei%7D 422 (Unprocessable Entity)

  • The url /automoveis/{automovei} is a pattern, you need to replace {automovei} for id of the car, getting /automoveis/5 for example.

  • The problem is just this, I can even pass the direct ID at the put that it gives the same error

  • Comment here what exactly is the error shown. Remembering that, the id car needs to be valid if not it throws an error that has not been found.

  • It does not process inputs, even passing the right url

  • Update the question with the Controller code, Request if you have so that I can help you better. If you are throwing any error, put also.

  • I put the controller methods in the question.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.