laravel - 为什么我在 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的新手。
解决方案
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');
});
希望这可以帮助
推荐阅读
- php - 执行八进制传递的函数
- regex - 如何使用 REGEX 在第一个点之前找到带有空格字符的多个点
- matlab - 如何将可选位置参数传递给matlab中的函数
- react-native - 如何在本机反应中从导航抽屉中调用类函数?
- c# - 将绑定值设置为 Xamarin Forms 中行为的参数
- android - 蓝牙应用程序未扫描可用设备
- c# - 在 Azure 函数中捕获异步方法异常
- asp.net-mvc - 如何使用 ASP.NET MVC 将值从一个项目传递到另一个项目?
- php - 使用 ACF 输出单个页眉样式和页脚脚本
- javascript - 固定响应式顶栏覆盖第二个固定顶栏