'Eager loading in Laravel 9 vs 5
I am upgrading a project from Laravel 5 to Laravel 9, and one of the Controller methods seems to break and I cant figure out why. It works perfectly fine in Laravel 5, I get all the models returned. It fails when trying to load the 'liveConfig' and 'draftConfig' models.
Error
PHP Error: Call to a member function addEagerConstraints() on null in /var/www/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php
Controller
public function show($siteId)
{
return auth()->user()->editing()->findOrFail($siteId)->augmented();
}
Relationships
public function liveConfig()
{
if (!$this->theme) {
return;
}
return $this->hasOne(LiveConfig::class)->where('status',
'live');
}
public function draftConfig()
{
if (!$this->theme) {
return;
}
return $this->hasOne(LiveConfig::class)->where('status',
'draft');
}
Eager Loading method
public function augmented()
{
$this->load([
'theme',
'theme.renewalPlans',
'subscription',
'subscription.nextSubscription',
'liveConfig',
'draftConfig',
])->append('open_invoices_count', 'cmsStyling', 'cmsStylesheets');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
