【问题标题】:Incompatible method with the override of setKeysForSaveQuery() on Laravel 8与 Laravel 8 上的 setKeysForSaveQuery() 覆盖不兼容的方法
【发布时间】: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'));
    }
   
}

【问题讨论】:

    标签: php laravel api laravel-8


    【解决方案1】:

    你需要让它匹配更改下面的代码

    protected function setKeysForSaveQuery(Builder $query){
    

    protected function setKeysForSaveQuery($query){
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 2011-11-28
      • 1970-01-01
      • 2018-07-07
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多