Advertisement
smallkan

Untitled

Mar 2nd, 2022
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <?php
  2.  
  3. public function store(Request $request)
  4.     {
  5.         $validated = $request->validate([
  6.             'nome' => 'required',
  7.             'tel' => 'required',
  8.             'cpf' => 'required',
  9.             'placa' => 'required'
  10.         ]);
  11.  
  12.         DB::beginTransaction();
  13.  
  14.         try {
  15.             //Espaço para tratar alguma lógica, ex:
  16.             $validated['tel'] = Helper::formatarTelefone($request->tel);
  17.  
  18.             //Nome de model não pode ser no plural
  19.             $newCliente = Clientes::create($validated);
  20.             DB::commit();
  21.  
  22.             return response()->json([
  23.                 'status' => true,
  24.                 'notification' => '... realizado com sucesso.',
  25.                 'data' => new ClientesResource($newCliente)
  26.             ], 201);
  27.         } catch (Exception $e) {
  28.             DB::rollback();
  29.            
  30.             return response()->json([
  31.                 'status' => false,
  32.                 'notification' => $e->getMessage()
  33.             ]);
  34.         }
  35.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement