douzoudang1511 2019-07-01 01:13
浏览 48

在某些情况下,Laravel关系会返回错误数据

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');

    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目
    • ¥20 mysql架构,按照姓名分表
    • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题