77 lines
2.6 KiB
PHP
77 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Jugador;
|
|
use Illuminate\Http\Request;
|
|
|
|
class JugadorController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$jugadores = Jugador::with('clubActual', 'clubOrigen', 'equipos')->get();
|
|
return response()->json($jugadores);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$jugador = Jugador::with('clubActual', 'clubOrigen', 'equipos')->findOrFail($id);
|
|
return response()->json($jugador);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'id_jugador' => 'nullable|string|max:6',
|
|
'documento' => 'required|string|max:20|unique:jugadores',
|
|
'nombre' => 'required|string|max:100',
|
|
'apellido' => 'required|string|max:100',
|
|
'fecha_nacimiento' => 'nullable|date',
|
|
'edad' => 'nullable|integer',
|
|
'categoria' => 'nullable|string|max:20',
|
|
'id_club_actual' => 'nullable|integer|exists:clubes,id_club',
|
|
'id_club_origen' => 'nullable|integer|exists:clubes,id_club',
|
|
'activo' => 'nullable|boolean',
|
|
'email' => 'nullable|email|max:150',
|
|
'telefono' => 'nullable|string|max:50',
|
|
'password' => 'nullable|string',
|
|
]);
|
|
if (isset($data['password'])) {
|
|
$data['password'] = bcrypt($data['password']);
|
|
}
|
|
$jugador = Jugador::create($data);
|
|
return response()->json($jugador, 201);
|
|
}
|
|
|
|
public function update(Request $request, $id)
|
|
{
|
|
$jugador = Jugador::findOrFail($id);
|
|
$data = $request->validate([
|
|
'documento' => 'sometimes|string|max:20|unique:jugadores,documento,' . $id . ',id_jugador',
|
|
'nombre' => 'sometimes|string|max:100',
|
|
'apellido' => 'sometimes|string|max:100',
|
|
'fecha_nacimiento' => 'nullable|date',
|
|
'edad' => 'nullable|integer',
|
|
'categoria' => 'nullable|string|max:20',
|
|
'id_club_actual' => 'nullable|integer|exists:clubes,id_club',
|
|
'id_club_origen' => 'nullable|integer|exists:clubes,id_club',
|
|
'activo' => 'nullable|boolean',
|
|
'email' => 'nullable|email|max:150',
|
|
'telefono' => 'nullable|string|max:50',
|
|
'password' => 'nullable|string',
|
|
]);
|
|
if (isset($data['password'])) {
|
|
$data['password'] = bcrypt($data['password']);
|
|
}
|
|
$jugador->update($data);
|
|
return response()->json($jugador);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$jugador = Jugador::findOrFail($id);
|
|
$jugador->delete();
|
|
return response()->json(null, 204);
|
|
}
|
|
}
|