Problem with PUT axios Laravel

0

I am using Laravel 5.6, routes and controller with resource and in the Post script the code works normally, however with the PUT it always gives error.

Follow the POST working code

        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' }}
        })

In the 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')];
    }
    
asked by anonymous 20.09.2018 / 20:23

1 answer

0

When you use Route::resource('automoveis') to generate your 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

So, the POST route works to register a Automovel but if you want to update the Automovel information, then you need to access the /automoveis/{automovel} route.

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

    
20.09.2018 / 20:45