Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FormatModel class to work with keyless Pivot models #1282

Merged
merged 3 commits into from
Dec 14, 2022

Conversation

Cellard
Copy link
Contributor

@Cellard Cellard commented Dec 13, 2022

We have two models with pivot between. It is very important for the case, that pivot has no primary key, driven with custom intermediate model and contains extra attributes:

class GameTeamPivot extends Pivot
{
    //
}
class Team extends Model 
{
    public function games(): BelongsToMany
    {
        return $this->belongsToMany(Game::class)
            ->using(GameTeamPivot::class)
            ->as('playing')
            ->withPivot('side', 'goals', 'players');
    }
}

As pivot has model, it generates events, that observes by Telescope.

In case then we update pivot attributes values, the updated event fires. And Telescope throws an Exception:

The attribute [id] either does not exist or was not retrieved for model [App\Models\Pivots\GameTeamPivot]. {"exception":"[object] (Illuminate\\Database\\Eloquent\\MissingAttributeException(code: 0): The attribute [id] either does not exist or was not retrieved for model [App\\Models\\Pivots\\GameTeamPivot]. at /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:474)
[stacktrace]
#0 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(454): Illuminate\\Database\\Eloquent\\Model->throwMissingAttributeExceptionIfApplicable('id')
#1 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1954): Illuminate\\Database\\Eloquent\\Model->getAttribute('id')
#2 /Users/pm/www/fcz/press/vendor/laravel/telescope/src/FormatModel.php(17): Illuminate\\Database\\Eloquent\\Model->getKey()
#3 /Users/pm/www/fcz/press/vendor/laravel/telescope/src/Watchers/ModelWatcher.php(54): Laravel\\Telescope\\FormatModel::given(Object(App\\Models\\Pivots\\GameTeamPivot))
#4 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(418): Laravel\\Telescope\\Watchers\\ModelWatcher->recordAction('eloquent.update...', Array)
#5 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(249): Illuminate\\Events\\Dispatcher->Illuminate\\Events\\{closure}('eloquent.update...', Array)
#6 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(189): Illuminate\\Events\\Dispatcher->dispatch('eloquent.update...', Array)
#7 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1210): Illuminate\\Database\\Eloquent\\Model->fireModelEvent('updated', false)
#8 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1121): Illuminate\\Database\\Eloquent\\Model->performUpdate(Object(Illuminate\\Database\\Eloquent\\Builder))
#9 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php(245): Illuminate\\Database\\Eloquent\\Model->save()
#10 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php(209): Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany->updateExistingPivotUsingCustomClass(1, Array, false)
#11 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php(187): Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany->updateExistingPivot(1, Array, false)
#12 /Users/pm/www/fcz/press/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php(114): Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany->attachNew(Array, Array, false)
#13 /Users/pm/www/fcz/press/app/Jobs/Offsite/GamesJob.php(105): Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany->sync(Array)

As you can see, Telescope tries to format model name, calling getKey on model. But pivot has no primary key.

This PR is to fix this error, suggesting way to format keyless pivot name.

@taylorotwell taylorotwell merged commit fcf4d36 into laravel:4.x Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants