I have a problem. I'm doing a crud with Ajax + Laravel. "Everything" is working, except that when I update a data, it also registers. Well, look what I'm doing. I have a Bhutan that I have an id called dashboard_character_category. This id is for me to retrieve with jQuery and trigger the click event.
<button id="dashboard_cadastrar_categoria" type="button" class="btn btn-primary butao_categoria">Cadastrar</button>
More when I go to update I change this id with jQuery to update_.
$('.butao_categoria').attr('id', 'update_');
When I update it updates and registers soon after. What could it be?
Ajax method to access the store method in my Category.php controller
$(document).ready(function () {
$('#success').hide();
$(document).ready(function () {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$("#dashboard_cadastrar_categoria").click(function () {
$.ajax({
url: '/administracao/categoria/store',
type: 'POST',
data: {_token: CSRF_TOKEN, nome: $("#nome_categoria").val()},
dataType: 'JSON',
success: function (data) {
// console.log(data);
$("#nome_categoria").val('');
$('#success').show();
$('#success').text('Categoria Cadastrada com Sucesso');
setTimeout(function () {
$('#success').fadeOut('slow');
}, 5000);
}
});
});
});
});
This method works the most I suspect that after I update it in this method I do not know why.
Medoto Ajax that accesses the Category.php controller to get the information to edit.
$(document).ready(function () {
$('.categoria_editar').click(function () {
var id = $(this).attr('value');
$.ajax({
url: '/administracao/categoria/edit/' + id,
type: 'GET',
dataType: 'JSON',
}).done(function (e) {
$('#cadastro_categoria').modal('show');
$('.butao_categoria').attr('id', 'update_');
$('.butao_categoria').text('Update');
$('#dashboard_categoria_id').val(e.id);
$('#nome_categoria').val(e.nome_categoria);
});
});
});
And finally the ajax method that accesses the controller to update
$(document).ready(function () {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$(".butao_categoria").click(function () {
var nome_catg = $('#nome_categoria').val();
var id_categ = $('#dashboard_categoria_id').val();
$.ajax({
url: '/administracao/categoria/update',
type: 'POST',
data: {_token: CSRF_TOKEN, nome_categoria: nome_catg, id_categ: id_categ},
dataType: 'JSON',
success: function (e) {
console.log(e);
}
});
});
});
Controller: Store Method:
public function store(Request $request){
if ($request->isMethod('POST')){
$categoria = new Categoria;
$categoria->create([
'nome_categoria' => $request->get('nome_categoria'),
]);
if($categoria){
return response()->json(['success' => 'success' , 'categoria' => Categoria::where('nome_categoria' , $request->get('nome_categoria'))->first()]);
}
}
return response()->json(['response' => 'This is get method']);
}
Edit Method
public function edit($id){
$categoria = Categoria::findOrFail($id);
return response()->json($categoria);
}
Update Method
public function update(Request $request){
$categoria = Categoria::findOrFail($request->get('id_categ'));
$result = $categoria->update([
'nome_categoria' => $request->get('nome_categoria'),
]);
if($result){
return response()->json('sucesso');
}
}