I'm doing my first steps in Laravel and migrating a Time Control application originally made in PHP.
The thing is that I have created a CRUD with Crud Generator but in the table where it shows the data I want it to take data from another table in the INNER_JOIN style with pluck when a condition is met.
In the ASSISTANCE table I have a person_code field that should show me some data when it matches the row of EMPLOYEES with a match in the employee_code field.
This is the code I have now but it crashes everywhere and I'm stuck. Any ideas?
public function index()
{
$asistencias = Asistencia::paginate();
$empleados = Empleado::where('codigo_empleado','asistencias.codigo_empleado')->pluck('nombre','apellidos');
return view('asistencia.index', compact('asistencias','empleados'))
->with('i', (request()->input('page', 1) - 1) * $asistencias->perPage());
}
Thanks in advance
Let's start with the query in this part.
This query returns a pagination that could be used to do it in the view but NOT in a query
On the other hand.
The use of the query builder in the where clause, the documentation tells us that we must pass the column of the table as the first argument, followed by the operator and finally the value with which the comparison must be made. Staying as:
If everything is well modeled in your database and in the models you have the relationships that Laravel provides you, you could do something like this:
This to obtain the employees that have a registered attendance in your database and, if you need to use conditionals within that relationship, you can pass an anonymous function to the whereHas method.
If you have doubts here is the laravel documentation