I have 2 tables in Laravel which have their relationships and they return the data correctly in most cases, the structures are something like this
Table workers
----------
ConsecutiveNomina| ConsecutiveWork| name| last_name
This table in the ConsecutiveNomina
field has the value1
for most of the employees and ConsecutiveWork
would be the autoincrementable field +1, the problem is when a worker has2
in the ConsecutiveNomina
field.
My second table has this structure
Table charges
----------
ConsecutiveNomina | ConsecutiveWork| position
Like the previous ConsecutiveNomina
it has the value1
for the majority and ConsecutiveWorker
would be the autoincrementable field +1
When an employee in the Workers
table has2
in ConsecutiveNomina
their relation to theCharges
table should return the data for the ConsecutiveNomina
2 and its number inConsecutiveWorkman
, for example
Table workers
----------
ConsecutiveNomina| ConsecutiveWork| name| last_name
----------
1 | 1 | Juan | Gomez
2 | 1 | Pedro | Perez
Table charges
----------
ConsecutiveNomina | ConsecutiveWork| position
----------
1 | 1 | employee
2 | 1 | Manager
This is how the data is distributed in the database, if ConsecutiveNomina
is1
ConsecutiveWork
is increasing +1, but if2
starts from 1 again only for those with 2
inConsecutiveNomina
In most cases it returns the data well, for example
$users = Employee::where([['Situacion', '0'], ['ConsecutiveNomina', '1']])->get();
foreach($users as $user){
$user->position->cargoRNET
}
This works perfectly, but if you want to do the same for the workers with ConsecutiveNomina
2 as follows
$users = Empleado::where([['Situacion', '0'], ['ConsecutiveNomina', '2']])->get();
foreach($users as $user){
$user->position->cargoRNET
}
In the relationship instead of returning Manager
which is what corresponds to this user, I returnEmployee
, since both Manager
andEmployee
share the same value of ConsecutiveWorkman
in this case would be for example1
, my question is how do I in my relationship bring me Manager
or the corresponding data when the worker has2
in ConsecutivoNomina
.
I suppose that at this moment the data returns me badly, since both share the same value of CosecutivoTrabajador
and I return the first one that finds, but I need that I return in the table of charges the value of ConsecutiveWorker = 1
, but with ConsecutiveNomina = 2
My models
class Employee extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'fldTimeStamp'
];
protected $connection = 'sqlsrv';
protected $table = 'IGNom_Trabajador';
protected $primaryKey = 'Consecutivo_Trabajador';
public function client(){
return $this->belongsTo('App\Client', 'ConsecutivoDpto');
}
public function position(){
return $this->belongsTo('App\Position', 'ConsecutivoTrabajador');
}
}
class Position extends Model
{
protected $connection = 'sqlsrv';
protected $table = 'IGNom_RNET';
protected $primaryKey = 'ConsecutivoTrabajador';
public function employee(){
return $this->hasMany('App\Employee');
}
}