首页 > 解决方案 > 如何使用 HasMany 显示数据数据表 Laravel

问题描述

这是我的控制器我想显示数据循环但是当它显示时只出现一个问题在哪里,请帮助,并给我解决这个问题的解决方案

if(empty($search)){
                $jobData = SrvJobSop::with('getJobSopFileMap')
                ->limit($limit)
                ->orderBy($order, $dir)
                ->get();
            }else{
                $jobData =  SrvJobSop::where('name', 'LIKE',"%{$search}%")
                ->offset($start)
                ->limit($limit)
                ->orderBy($order,$dir)
                ->get();

                $totalFiltered =  SrvJobSop::where('name', 'LIKE',"%{$search}%")
                ->count();
            }

            $data = [];
            if(count($jobData) > 0){
                foreach($jobData as $num => $jobsop){
                    $currentData = [];
                    $currentData['no'] = $num+1;
                    $currentData['sop_code'] = $jobsop->sop_code;
                    $currentData['name'] = $jobsop->name;
                    $currentData['vehicle_brand_id'] = $jobsop->getVehicleBrand->name;
                    $currentData['vehicle_model_id'] = $jobsop->getVehicleModel->name;
                    $currentData['vehicle_type_id'] = $jobsop->getVehicleType->name;
                    $currentData['year_id'] = $jobsop->getYear->name;
                        foreach($jobsop->getJobSopFileMap as $p){
                            $currentData['file'] = $p->file;
                        }
                    $currentData['machine_type_id'] = $jobsop->getMachineType->name;
                    $currentData['machine_type_id'] = $jobsop->getMachineType->name;
                    $currentData['created_at']      = date('d F Y h:m:s',strtotime($jobsop->created_at));
                    $currentData['created_by_user_id']     = $jobsop->getCreatedByUserId->name;
                    $currentData['opsi'] = "
                    <a href=".url('/carfix-workshop-operation-module/job-sop/form?id='.encrypt($jobsop->srv_job_sop_id)).".  class='btn btn-success btn-xs fa fa-pencil-square-o'
                    title='Edit Data'></a>

                    <a onclick=deleteData('".encrypt($jobsop->srv_job_sop_id)."') class='btn btn-danger btn-xs fa fa-trash-o'
                    title='Delete Data'></a>";

                    $data[] = $currentData;
                }[enter image description here][1]
            }

我想循环文件名怎么做,我的代码对吗?

标签: laraveldatatablesrelationshiphas-manyrelation

解决方案


推荐阅读