Incompatible method with the override of setKeysForSaveQuery() on Laravel 8

Viewed 1136

We created three tables called 'users', 'corso', 'iscrizione' which is a middle table between users and corso, it has two attributes that are PK/FK.
We successfully built the methods to create/update/read/delete rows inside 'corso' and we populated manually 'users'.
We are trying to realize a create method for iscrizione but when we try to test it on Postman it gives us an error: Illegal offset type

We learned that Eloquent doesn't support composite keys (which is a shame) and the only way to bypass this limit is to override the method setKeysForSaveQuery() from Eloquent inside the model of our table.
Sadly it gives us this error:

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

Iscrizione Model

<?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'));
    }
   
}
1 Answers

You need to make it match change the below code

protected function setKeysForSaveQuery(Builder $query){

to

protected function setKeysForSaveQuery($query){
Related