Search code examples
jsonlaravel-8relational-databaselaravel-api

Unable to fetch data from relational table using laravel with() method


I have table "Jobs" having a field called "empids". we are storing comma separated employee ids in this field, for ex. 1,12,56. I am trying to return JSON using with() method by making belongsTo relation ship between Job and employee models. There are more relation ship tables working fine with Jobs table using belongsTo as they are perfectly matching forien_key and index_key.

Below is my laravel code

// Get Job function:
       $emps = job::where('id',$id)->where('compid',$compid)->pluck('empids');
        $jobs = job::with(['jobcustomer'=>function ($q) use ($compid){
            $q->where('deleted_at',null)->where('compid',$compid);
        }])
        ->with(['jobcustomeraddress'=>function ($q) use ($compid){
            $q->where('deleted_at',null)->where('compid',$compid);
        }])
        ->with(['jobdetails'=>function ($q) use ($compid){
            $q->where('deleted_at',null)->where('compid',$compid);
        }])
        ->with(['jobemps'=>function ($q) use ($emps) { $q->whereIn('id',$emps); }])
        ->where('id',$id)->where('compid',$compid)->orderBy('id')->get();
        return $jobs;

//In Job Model:
public function jobemps()
    {
        return $this->belongsTo(employee::class);
    }

Below is what i am receiving as JSON. "Jobemps" are null

[
    {
        "id": 71,
        "invid": "3",
        "segid": "9",
        "segname": "segment # 9",
        "due": "Upon start of work",
        "empids": "1,2,12",
        "invcurrstatus": null,
        "customer_id": 7,
        "custaddresse_id": 48,
      
        "jobcustomer": {
            "id": 7,
            "fname": "fawaz",
            "lname": "chughtai",
            "dname": "lat long",
            "email": "[email protected]",
        },
        "jobcustomeraddress": {
            "id": 48,
            "customer_id": 7,
            "street": "82 NJ-23",
            "city": "Hamburg",
            "state": "NJ",
            "zip": "07419-1400",
            "unit": null,
        },
        "jobdetails": [
            {
                "id": 351,
                "job_id": 71,
                "invid": "3",
                "segid": "9",
                "itemsec": 1,
                "invitem_id": 1,
                "itemname": "Service Call",
                "itemunit": "Each",
                "itemcost": 0,
                "itemprice": 69,
                "itemqty": 1,
                "itemtotal": 69,
            },
            {
                "id": 352,
                "job_id": 71,
                "invid": "3",
                "segid": "9",
                "itemsec": 2,
                "invitem_id": 2,
                "itemname": "Repair - Unclog toilet",
                "itemdesc": "Unclog a toilet. Must be reasonably accessible.",
                "itemunit": "Each",
                "itemcost": 0,
                "itemprice": 71,
                "itemqty": 1,
                "itemtotal": 71,
            }
        ],
        "jobemps": null
    }
]

I am looking for jobemps as:

"jobemps": [
            {
                "id": 1,
                "name": "abc",
            },
            {
                "id": 12,
                "name": "efg",
            },
            {
                "id": 56,
                "name": "xyz",
            }
        ],

Error i am receiving is select * from employees where 0 = 1 and id in (1,2,12)

I dont know how to remove where 0 = 1 Please guide, Thanks


Solution

  • OK So based on Pemba Tamang's answer, it does solved the issue, here is the 100% working code with little fixes in Pemba's code

    $emps = (job::where('id',$id)->where('compid',$compid)->first())->empids;
            $emps = explode(',',$emps);
            
            $jobs = job::with(['jobcustomer'=>function ($q) use ($compid){
                $q->where('deleted_at',null)->where('compid',$compid);
            }])
            ->with(['jobcustomeraddress'=>function ($q) use ($compid){
                $q->where('deleted_at',null)->where('compid',$compid);
            }])
            ->with(['jobdetails'=>function ($q) use ($compid){
                $q->where('deleted_at',null)->where('compid',$compid);
            }]) 
            ->where('id',$id)->where('compid',$compid)->first();
            
    
            $jobemps = employee::whereIn('id',$emps)->get();
            $data['job'] = $jobs;
            $data['jobemps'] = $jobemps;
            return $data;
    

    Now i dont know if I should Mark Pemba's answer as accepted or mine, or may be Pemba corrects his answer so that I mark his answer as accepted. Thanks Pemba