与 Laravel 8 上的 setKeysForSaveQuery() 重写不兼容的方法

Pas*_*sha 3 php api laravel laravel-8

我们创建了三个表,分别为“users”、“corso”、“iscrizione”,这是users和corso之间的中间表,它有两个属性:PK/FK。
我们成功构建了在“corso”内创建/更新/读取/删除行的方法,并手动填充了“用户”。
我们正在尝试实现 iscrizione 的创建方法,但是当我们尝试在 Postman 上测试它时,它给了我们一个错误: Illegal offset type

我们了解到 Eloquent 不支持复合键(这是一个遗憾),绕过此限制的唯一方法是setKeysForSaveQuery()在我们的表模型中重写 Eloquent 的方法。
不幸的是,它给了我们这个错误:

Method 'App\Models\Iscrizione::setKeysForSaveQuery()' is not compatible with method 'Illuminate\Database\Eloquent\Model::setKeysForSaveQuery()'.intelephense(1038)
Run Code Online (Sandbox Code Playgroud)

伊斯克里齐奥内模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

class Iscrizione extends Model
{
    use HasFactory;

    protected $hidden = ['idCorso', 'idUtente'];
    protected $table = 'iscrizione';

    protected function setKeysForSaveQuery(Builder $query){

        return $query->where('idCorso', $this->getAttribute('idCorso'))
            ->where('idUtente', $this->getAttribute('idUtente'));
    }
   
}
Run Code Online (Sandbox Code Playgroud)

小智 6

您需要使其匹配更改以下代码

protected function setKeysForSaveQuery(Builder $query){
Run Code Online (Sandbox Code Playgroud)

protected function setKeysForSaveQuery($query){
Run Code Online (Sandbox Code Playgroud)