【发布时间】:2021-06-03 10:30:53
【问题描述】:
我们创建了三个表,分别称为“users”、“corso”、“iscrizione”,这是用户和 corso 之间的中间表,它具有 PK/FK 两个属性。
我们成功构建了创建/更新/读取/删除 'corso' 中的行,我们手动填充了 'users'。
我们正在尝试实现 iscrizione 的创建方法,但是当我们尝试在 Postman 上对其进行测试时,它给了我们一个错误:???? Illegal offset type
我们了解到 Eloquent 不支持复合键(这很遗憾),绕过此限制的唯一方法是在我们的表模型中覆盖 Eloquent 中的 setKeysForSaveQuery() 方法。
遗憾的是它给了我们这个错误:
Method 'App\Models\Iscrizione::setKeysForSaveQuery()' is not compatible with method 'Illuminate\Database\Eloquent\Model::setKeysForSaveQuery()'.intelephense(1038)
Iscrizione 模型
<?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'));
}
}
【问题讨论】: