Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mcadio's avatar

Db relations not loading on remote db

I've got identical code on my local and server machines. The local schedules people, and now my remote won't load appointment relations for a day and is erroring out.

public function getTodaysHours($apptDate, $officehour_id = null, $doctor_id = null) {
        $oh = Officehour::with(
            ['closed' => function($query) use ($apptDate) {
                $query->apptDate($apptDate)->first();
            },
                'appts' => function($query) use ($apptDate) {
                    $query->apptDate($apptDate)->get();
                }
            ])
            ->apptDate($apptDate)
            ->doctorId($doctor_id)
            ->get();

        // if officehour id
        $hrs = $officehour_id ? $oh->where('id',$officehour_id)->first() : $oh->first();
        return $hrs->closed->count() > 0 ? $hrs->closed->first() : $hrs;;
    }

It isn't loading relations on the server. Here is what I get when I load what that function returns when using the remote db:

App\Models\Closed {#2161 ▼
  #connection: "mysql"
  #table: "closed"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:10 [▼
    "id" => 312
    "closed_date" => "2023-12-09"
    "AM_PM_DAY" => null
    "reason" => null
    "open" => "08:00:00"
    "close" => "10:50:00"
    "deleted_at" => null
    "doctor_id" => 2
    "closed" => 0
    "officehour_id" => 8
  ]
  #original: array:10 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: array:3 [▶]
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:8 [▶]
  #guarded: array:1 [▶]
}

I host on digital ocean managed db.

0 likes
2 replies
LaryAI's avatar
Level 58

It seems like there might be a couple of issues with the code provided. One potential issue is the use of first() and get() methods inside the eager loading constraints. These methods should not be used within the closure provided to the with method because they will execute the query immediately, which is not how eager loading is supposed to work. Instead, you should only define constraints within the closure and let Laravel handle the loading of the relationships.

Here's a revised version of the getTodaysHours method:

public function getTodaysHours($apptDate, $officehour_id = null, $doctor_id = null) {
    $query = Officehour::query();

    // Define the relationships with constraints but do not execute them here
    $query->with([
        'closed' => function($query) use ($apptDate) {
            $query->where('apptDate', $apptDate);
        },
        'appts' => function($query) use ($apptDate) {
            $query->where('apptDate', $apptDate);
        }
    ]);

    // Apply additional constraints to the Officehour query
    $query->where('apptDate', $apptDate);

    if ($doctor_id) {
        $query->where('doctor_id', $doctor_id);
    }

    // Execute the query and get the results
    $oh = $query->get();

    // Filter by officehour_id if provided
    $hrs = $officehour_id ? $oh->where('id', $officehour_id)->first() : $oh->first();

    // Check if there are any closed entries and return accordingly
    return $hrs && $hrs->closed && $hrs->closed->count() > 0 ? $hrs->closed->first() : $hrs;
}

Make sure that the apptDate scope on your Officehour model is correctly defined to add the necessary where clause to the query.

Additionally, ensure that your database structure and data on the remote server match what you have locally. Sometimes, issues can arise if there are discrepancies between the local and remote databases, such as missing tables, columns, or different data that could affect the query results.

If the problem persists after these changes, you may want to check the server's error logs for more detailed information about the error that's occurring. It's also a good idea to ensure that your server environment meets all the requirements for the Laravel version you are using.

Please or to participate in this conversation.