Fetching data from polymorphic relation in laravel

20 views Asked by At

i have three tables that has poly morphic relationship, maintenance: -id -stock_SNW_issue_id

Obsolescence: -id -stock_SNW_issue_id

Staff_performance -id -performance_id -performance_type

I want to get id from maintenance table where stock_SNW_issue_id = $issueId, and check whether that id exists on the staff_performance table or not.

This is my function inside controller:

public function isIssueAssigned($issueId)
    {
        $bio_id = bio_md_maintenance_dealer::where('stock_SNW_issue_id', $issueId)->value('id');

        $isAssigned = Staff_Performances::whereHas('performance', function ($subQuery) use ($bio_id) {
            $subQuery->where('performance_id', $bio_id)
                ->where('performance_type', 'App\Models\Hospitals\MaintenanceReport\bio_md_maintenance_dealer');
        })->exists();

        return response()->json(['isAssigned' => $isAssigned]);
    }

this is my staff_performance model:

public function performance()
    {
        return $this->morphTo();
    }

this is maintenance model:

public function staffPerformances()
    {
        return $this->morphMany(Staff_Performances::class, 'performance');
    }

this is obsolescence model:

public function staffPerformances()
    {
        return $this->morphMany(Staff_Performances::class, 'performance');
    }

I got two issues: 1- This code runs infinitively 2- gives performance_id column not found error.

1

There are 1 answers

0
Nikunj Gadhiya On

The infinite loop issue in your code seems to be due to the way you're using morph relationships and eager loading. Also, the "column not found" error is likely because the Staff_Performances model doesn't have a column named performance_id. Instead, it's using polymorphic relations. Here's how you can refactor your code to address both issues:

First, ensure that your Staff_Performances migration has the necessary columns for polymorphic relations, typically performance_id, performance_type, and other relevant columns.

Assuming your Staff_Performances model is correctly set up with morphTo relations, here's how you can refactor your isIssueAssigned function:

use App\Models\Staff_Performances;
use App\Models\Hospitals\MaintenanceReport\bio_md_maintenance_dealer;

public function isIssueAssigned($issueId)
{
    // Get the maintenance record ID based on the issue ID
    $maintenanceId = bio_md_maintenance_dealer::where('stock_SNW_issue_id', $issueId)->value('id');

    // Check if the maintenance record is assigned in staff performance
    $isAssigned = Staff_Performances::where('performance_id', $maintenanceId)
        ->where('performance_type', 'App\Models\Hospitals\MaintenanceReport\bio_md_maintenance_dealer')
        ->exists();

    return response()->json(['isAssigned' => $isAssigned]);
}

Ensure that you have imported the necessary models at the top of your controller.

This refactored code directly queries the Staff_Performances table for the existence of a record where the performance_id matches the maintenance record ID and the performance_type matches the type of the maintenance record. This should avoid the infinite loop issue and resolve the "column not found" error.

Remember to adjust the namespaces and class names as per your actual application structure.