首页 > 解决方案 > 为什么我在 Laravel 中的 getKey() 方法后收到 Null 返回

问题描述

我有一个数据库,一个BranchSummaryReport模型,在这个实体上,

我放了一个protected $primaryKey = 'product_id';. 但是,如果您转到 branch_summary_report 表,您会发现 branch_summary_report 上的这个“primary_id”列不是primary_key。并且“product_id”具有重复数据(第 1 行和第 2 行的 product_id 为 1,但 branch_id 不同)。

当我使用这个 getKey() 方法时,我收到了一个 Null 而不是我将收到它的某行的 product_id(s)。不知道是不是因为这个getKey方法不支持多个id。

谢谢,我是laravel的新手。

标签: laravellaravel-5eloquent

解决方案


product_id必须是branch_summary_report表迁移中的主键,像这样使它成为主键

Schema::create('branch_summary_report', function (Blueprint $table) {
    // Can't have two incrementing IDs, you can only have 1 primary key
    // $table->bigIncrements('id');
    $table->bigIncrements('product_id');
});

希望这可以帮助


推荐阅读