我们如何使用 apiresources laravel 将多个关系合并到一维数组中

Posted

技术标签:

【中文标题】我们如何使用 apiresources laravel 将多个关系合并到一维数组中【英文标题】:how can we merge multiple relation into one dimensional array using apiresources laravel 【发布时间】:2021-01-26 17:38:09 【问题描述】:

关系定义如下:

class Rfid extends Model

    use HasFactory;

    public function logs()
    
        return $this->hasMany('App\Models\ComponentLog', 'rfid_id');
    


class ComponentLog extends Model

    use HasFactory;

    public function reader()
    
        return $this->belongsTo('App\Models\RfidReader','rfid_reader_id');
    


class RfidReader extends Model implements AuthenticatableContract

    use HasFactory;
    use HasApiTokens;
    use Authenticatable;

    public function department()
    
        return $this->belongsTo('App\Models\Department', 'department_id');
    

在查询Rfid::with('logs.reader.department')->get() 时,结果如下:

App\Models\Rfid #4554
         id: 13,
         RFID: "KDtCgimCJJ",
         department_id: 6,
         component_id: 13,
         created_at: "2020-10-12 10:48:32",
         updated_at: "2020-10-12 10:48:32",
         logs: Illuminate\Database\Eloquent\Collection #4599
           all: [
             App\Models\ComponentLog #4576
               id: 13,
               rfid_id: 13,
               check_in: "2020-10-12 10:48:32",
               check_out: null,
               rfid_reader_id: 4,
               created_at: null,
               updated_at: null,
               reader: App\Models\RfidReader #4421,
             ,
           ],
         ,
       ,...

但是,我希望使用 apiresources 合并它的关系。

【问题讨论】:

每个 Rfid 都有许多日志。如何附加logs 表的字段? 他们可以通过任何方式使用 apiresources... 能贴出你想要的对象的结构吗? 我希望它为 RFID,DEPARTMENT_NAME,RFID_READER_ID,CHECK_IN,CHECK_OUT 【参考方案1】:

第一步:定义一个ComponentLog -> Rfid belongsTo 关系

class ComponentLog extends Model

    use HasFactory;

    public function reader()
    
        return $this->belongsTo('App\Models\RfidReader','rfid_reader_id');
    

    public function rfid()
    
        return $this->belongsTo('App\Models\Rfid', 'rfid_id');
    

第二步:从ComponentLog中获取结果,避免hasMany关系

$logs = ComponentLog::with(['rfid', 'reader.department'])->get();

第 3 步:绘制结果

$logs->transform(function ($log) 
    return [
        'rfid' => $log->rfid->rfid,
        'department_name' => $log->reader->department->department_name,
        'rfid_reader_id' => $log->rfid_reader_id,
        'check_in' => $log->check_in,
        'check_out' => $log->check_out
    ];
);

你也可以内联。

$logs = ComponentLog::with(['rfid', 'reader.department'])
->get()
->transform(function ($log) 
    return [
        'rfid' => $log->rfid->rfid,
        'department_name' => $log->reader->department->department_name,
        'rfid_reader_id' => $log->rfid_reader_id,
        'check_in' => $log->check_in,
        'check_out' => $log->check_out
    ];
);

【讨论】:

以上是关于我们如何使用 apiresources laravel 将多个关系合并到一维数组中的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Route apiResource(apiResource 和 route 中资源的区别)

API 平台:具有嵌套实体的组仅在删除 @ApiResource 时有效

Laravel Route::apiResource Post 查询无法正常工作

如何在子目录 Laravel 中正确配置 htaccess?

larave学习笔记1-安装配置

使用Larave5.6l提交POST请求出现The page has expired due to inactivity错误