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