Skip to content

Commit a479984

Browse files
committed
Añadir input busqueda para incidencias
1 parent 1a3a3fb commit a479984

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

app/Http/Controllers/IncidenceController.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,16 @@ public function __construct(DocumentService $documentService)
1818
$this->documentService = $documentService;
1919
}
2020

21-
public function index()
21+
public function index(Request $request)
2222
{
23-
$incidences = Incidence::getIncidences();
23+
$busqueda = $request->query('busqueda');
24+
25+
if($busqueda) {
26+
$incidences = Incidence::getIncidences($busqueda);
27+
} else {
28+
$incidences = Incidence::getIncidences();
29+
}
30+
2431
return view('incidencias', compact('incidences'));
2532
}
2633

app/Models/Incidence.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,26 @@ public static function getIncidences (?string $busqueda = null)
1515
try { // Seleccionar id, titulo, fecha, nombre de la persona y rol (alumno o profesor)
1616
$incidences = DB::table('incidences')
1717
->select(DB::raw('HEX(incidences.id) as id'), 'incidences.titulo', 'incidences.fecha', 'incidences.incidenceable_type as rol', DB::raw('COALESCE(teachers.nombre, students.nombre) AS nombre,
18-
COALESCE(teachers.apellidos, students.apellidos) AS apellidos'))
19-
->leftJoin('teachers', function (JoinClause $join) {
18+
COALESCE(teachers.apellidos, students.apellidos) AS apellidos'))
19+
->leftJoin('teachers', function (JoinClause $join) {
2020
$join->where('incidences.incidenceable_type', '=', DB::raw("'Profesor'"))->on('incidences.incidenceable_id', '=', 'teachers.id');
21-
})
21+
}) // Las comparaciones con valores estáticos como 'Profesor' debe hacerse con DB::raw()
2222
->leftJoin('students', function (JoinClause $join) {
2323
$join->where('incidences.incidenceable_type', '=', DB::raw("'Alumno'"))->on('incidences.incidenceable_id', '=', 'students.id');
24-
})
25-
->get();
26-
// Las comparaciones con valores estáticos como 'Profesor' debe hacerse con DB::raw()
24+
});
2725

26+
if($busqueda) {
27+
$incidences = $incidences->whereAny(['incidences.titulo', 'students.nombre', 'students.apellidos', 'teachers.nombre', 'teachers.apellidos'], 'like', "%$busqueda%");
28+
}
29+
30+
$incidences = $incidences->get();
31+
2832
/* SELECT incidences.id, incidences.titulo, incidences.fecha, incidences.incidenceable_type,
29-
COALESCE(teachers.nombre, students.nombre) AS nombre,
30-
COALESCE(teachers.apellidos, students.apellidos) AS apellidos
31-
FROM `incidences`
32-
LEFT JOIN `teachers` ON incidences.incidenceable_type = 'Profesor' AND incidences.incidenceable_id = teachers.id
33-
LEFT JOIN `students` ON incidences.incidenceable_type = 'Alumno' AND incidences.incidenceable_id = students.id; */
33+
COALESCE(teachers.nombre, students.nombre) AS nombre,
34+
COALESCE(teachers.apellidos, students.apellidos) AS apellidos
35+
FROM `incidences`
36+
LEFT JOIN `teachers` ON incidences.incidenceable_type = 'Profesor' AND incidences.incidenceable_id = teachers.id
37+
LEFT JOIN `students` ON incidences.incidenceable_type = 'Alumno' AND incidences.incidenceable_id = students.id; */
3438

3539
return $incidences;
3640
} catch (Exception $e) {

0 commit comments

Comments
 (0)