【问题标题】:I'm getting an error when trying to save a new model with 2 fields referencing different ids in the same table尝试保存具有 2 个字段的新模型引用同一个表中的不同 ID 时出现错误
【发布时间】:2022-01-10 08:28:34
【问题描述】:

我是 laravel 的新手,我学习了使用迁移、模型和控制器创建、更新和删除数据库条目的基本工作流程。但是现在我正在尝试对包含subscriberId 和followeeId 的订阅表执行相同的操作。这两个字段都引用同一个表(用户)的不同 ID。这种任务似乎需要一些微调。我被困住了。

这是我的一些 cmets 代码。

订阅表

Schema::create('subscriptions', function (Blueprint $table) {
    $table->id();
    $table->unsignedBigInteger('subscriberId');
    $table->unsignedBigInteger('followeeId');
    $table->foreign('subscriberId')->references('id')->on('users');
    $table->foreign('followeeId')->references('id')->on('users');
});

以前,我使用了另一种方法来处理外部 id,即具有 $table->foreignId('user_id')->constrained() 模式的方法,但在这种特殊情况下,我需要确保两个外部 id 引用不同的用户,所以我选择了更多详细选项。

用户模型

public function subscriptions()
{
    return $this->hasMany(Subscription::class, 'subscriberId');
}

这里我添加了第二个参数。这似乎有效。

订阅模式

   class Subscription extends Model
   {
    use HasFactory;

    protected $fillable = [
        'subscriberId',
        'followeeId'
    ];

    public function subscriberId()
    {
        return $this->belongsTo(User::class, 'id', 'subscriberId');
    }

    public function followeeId()
    {
        return $this->belongsTo(User::class, 'id', 'followeeId');
    }
}

在这里我也传递了其他参数,尽管在这种情况下我不太确定这些参数是否正确。但这是我最好的猜测。如果我没记错的话,belongsTo 关系的第二个参数是从 传入 的模型推断出来的,而不是像 hasMany 关系那样是父类的模型。所以在这种情况下,这将是 users 表的“id”,无论如何这将是这里的默认值,但我需要第三个参数,所以我也明确说明了第二个参数。同样,我不确定这种组合,但这就是我能够制作的文档。我还使用了其他参数的其他组合,甚至尝试完全摆脱这两个公共函数,但这也行不通。

现在,这是控制器。如果我这样做:

$user->subscriptions()->get();

我确实得到了我想要的订阅。但如果我这样做:

$user->subscriptions()->create([
        'subscriberId' => 1,
        'followeeId' => 2
    ]);

我收到 500 错误。我还尝试了另一种方法:

    $newSub = new Subscription;
    $newSub->subscriberId = 1;
    $newSub->followeeId = 2;
    $newSub->save();
    return $newSub;

但仍然没有成功。当我尝试保存()时,我仍然收到 500 错误

请帮帮我。

【问题讨论】:

  • 在我们需要知道 500 错误的错误消息之前,请检查响应或日志。
  • 哦,好的。我在前端使用 React。以下是记录到控制台的内容: app.js:1612 POST 127.0.0.1:8000/api/users/1/subscribe 500 (Internal Server Error) dispatchXhrRequest @ app.js:1612 xhrAdapter @ app.js:1438 dispatchRequest @ app.js:2143 request @ app.js: 1914 Axios.@app.js:1946 换行@app.js:2558 handleForm@resources_js_compone…ogin_index_js.js:32 callCallback@app.js:26881 invokeGuardedCallbackDev@app.js:26930 invokeGuardedCallback@app.js:26992invokeGuardedCallbackAndCatchFirstError @app.js:27006 executeDispatch @app.js:31179
  • processDispatchQueueItemsInOrder @ app.js:31211 processDispatchQueue @ app.js:31224 dispatchEventsForPlugins @ app.js:31235 (匿名) @ app.js:31444 batchedEventUpdates$1 @ app.js:45327 batchedEventUpdates @ app. js:26681 dispatchEventForPluginEventSystem @ app.js:31443 attemptToDispatchEvent @ app.js:28941 dispatchEvent @ app.js:28860 stable_runWithPriority @ app.js:59263 runWithPriority$1 @ app.js:34212discreteUpdates$1 @ app.js:45344discreteUpdates @app .js:26692 dispatchDiscreteEvent@app.js:28825
  • 后端出现500错误
  • 好的。这是简短版本: PDOException: SQLSTATE[HY000]: General error: 1 table subscriptions has no column named updated_at in laravel\framework\src\Illuminate\Database\Connection.php:486

标签: php laravel laravel-8


【解决方案1】:

解决方案

我应该用过

public $timestamps = false

在订阅模型中,我也误解了文档。正确的组合是

用户模型

public function subscriptions()
{
    return $this->hasMany(Subscription::class, 'subscriberId');
}

订阅模式

public function subscriberId()
{
    return $this->belongsTo(User::class, 'subscriberId');
}

public function followeeId()
{
    return $this->belongsTo(User::class, 'followeeId');
}

【讨论】:

    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 2019-02-05
    相关资源
    最近更新 更多