I made this function so that it takes a request from js and at the time of entering the name of a category, give a select in the database, check if the name already exists and if it returns 0 in the count, it will write. >
You are recording, but when I enter the same name, it still does not respect the condition. Can anyone tell me why?
public function store(Request $request)
{
try{
$compara = DB::select("SELECT nome FROM categorias WHERE nome = '.$request->nome.'");
if(count($compara) == 0)
{
$categoria = new Categoria();
$categoria->fill($request->json()->all());
$categoria->save();
return response()->json($categoria, 201); //201 - created
}
}
catch(\Exception $ex){
return response()->json(["error"=>$ex->getMessage() . ' on line: ' . $ex->getLine()], 500);
}
}