-1
I am helping to develop a web application in Laravel for a company. It has several Modals included in them (including modal that leads to another modal).
To call these modals, I am using Jquery, as the example below:
$('.classe-do-modal').on('click', function(e) {
e.preventDefault();
let modal = $('#idDoModal');
modal.modal({show : true});
});
In my controller, I am returning the values in JSON format within my function show.
public function show($id){
$variavelModel = Model::where('outro_model_id',$id)->orderBy('created_at', 'desc')->get();
return response()->json($variavelModel);
}
In another Javascript file, you also have the following commands:
let p = JSON.parse($(this).attr('data-entity'));
if(p.id) {
$.ajax({
url: 'url/daRota/'+p.id,
type: "GET",
data : {"_token":"{{ csrf_token() }}"},
dataType: "json",
success:function(data) {
if(data){
$('#id-da-div').empty();
$.each(data, function(key, value){
$('#id-da-div').append(value);
});
}
}
});
}
And to register the data, I have as the example below:
public function store(Request $request){
MeuModel::create(array_merge($request->all(, ['outro_model_id'=>$request->outro_model_id]));
$variavel_model = MeuModel::where('outro_model_id',$request->outro_model_id)->get()->count();
OutroModel::where('id', $request->outro_model_id)->update('outro_model', $variavel_model);
alert()->success('Sucesso', 'cadastrada com sucesso!');
return back();
}
Above there is only one example of how I am registering the data I want. I am using the database.
But the main question is this. Is it correct to use the database to register and then remove the data from the database and turn it to JSON? As an example using an AJAX request to get use of my route in Laravel? At first, I am not able to pull the data directly from the database without the AJAX request. I believe this is because of the modals. It is correct to use this way to get the data by mixing JSON with Mysql?
And what problem are you thinking you might have? Why do you think one thing has to do with another?
– Maniero
I’m not sure if this method would be a "gambiarra" or an efficient method to solve the problem.
– Arthur Abitante
It is correct to use the database to register and then remove the data from the database and turn it to JSON?, I don’t know if I understood your doubt well but the database saves the data in its own format that doesn’t matter to the code (even if the database can save in json), because it returns the data and you must transform them in what is necessary to present, be it json or text, csv, a table or something, so I don’t see any problems there. Now this and Ajax are completely different things in your question
– Ricardo Pontual
Thanks @Ricardopunctual, clarified my question. Already in question to Ajax, I’m still confused with its functionality. I know it makes sense in matters like requisitions. But still in my head I am assimilating a lot to the JSON format because it is what I see most regarding an explanation of this functionality. In online tutorials for example I see some cursinhos using JSON as an example to explain AJAX.
– Arthur Abitante