Eloquent 对某些表使用了错误的键
Eloquent using wrong key for some tables
使用:Laravel 5.5
我使用一些地址元素(如:地区、区域、邮编等)作为下拉菜单和一些用户输入来构建地址。
我有 5 个地址元素,其中一个架构是:
Schema::create('address_districts', function (Blueprint $table) {
$table->increments('id');
$table->integer('admin_id')->unsigned();
$table->string('name');
$table->timestamps();
$table->foreign('admin_id')->references('id')->on('admins');
});
这是针对地区的,我还有另外 3 个与此完全相同的名称,address_thanas
、address_areas
、address_building_names
& address_zips
;
最后一个的唯一例外是 code
而不是其他 table 上的 name
:
Schema::create('address_zips', function (Blueprint $table) {
$table->increments('id');
$table->integer('admin_id')->unsigned();
$table->string('code'); // Look other table has name here........
$table->timestamps();
$table->foreign('admin_id')->references('id')->on('admins');
});
我将构建的地址存储在名为 addresses
的 table 上
Schema::create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->integer('district_id')->unsigned();
$table->integer('thana_id')->unsigned();
$table->integer('area_id')->unsigned();
$table->integer('zip_id')->unsigned();
$table->integer('building_name_id')->nullable()->unsigned();
$table->string('building');
$table->integer('floor');
$table->string('apt')->nullable();
$table->text('comment')->nullable();
$table->timestamps();
$table->foreign('district_id')->references('id')->on('address_districts');
$table->foreign('thana_id')->references('id')->on('address_thanas');
$table->foreign('area_id')->references('id')->on('address_areas');
$table->foreign('zip_id')->references('id')->on('address_zips');
$table->foreign('building_name_id')->references('id')->on('address_building_names');
});
在 Address
模型中,我定义了如下关系:
public function district() {
return $this->belongsTo(AddressDistrict::class, 'district_id');
}
public function thana() {
return $this->belongsTo(AddressThana::class, 'thana_id');
}
public function area() {
return $this->belongsTo(AddressArea::class, 'area_id');
}
public function building_name() {
return $this->belongsTo(AddressBuildingName::class, 'building_name_id');
}
public function zip() {
return $this->belongsTo(AddressZip::class, 'zip_id', 'id');
}
然后当我尝试使用 Address::create($data)
创建一个新地址时
我收到错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select count(*) as aggregate from `address_zips` where `name` = 2)
在这里我们可以看到它正在比较键 name
而不是 id
我注意到这不是报告无法插入数据或类似的东西,它无法计算相关模型并因此中止数据插入
这是为什么?
但奇怪的是我可以通过(手动插入一行到数据库中进行测试以查看是否可以检索数据)检索数据
$addresses = Address::orderByDesc('created_at')->get();
//loop as $address
$address->district->name
$address->zip->code
...
& 这很完美
当我创建新记录时,我需要这样的查询:
select count(*) as aggregate from `address_zips` where `id` = 2
任何帮助将不胜感激。
感谢您阅读这么长的问题。
问题出在您的 $data
数组中。可能您复制了一个带有输入 name
的表单,您需要将其重命名为 code
以便自动保存值。
或者,您可以手动设置输入
$address = new Address();
$address->code = request('name');
$address->save();
使用:Laravel 5.5
我使用一些地址元素(如:地区、区域、邮编等)作为下拉菜单和一些用户输入来构建地址。
我有 5 个地址元素,其中一个架构是:
Schema::create('address_districts', function (Blueprint $table) {
$table->increments('id');
$table->integer('admin_id')->unsigned();
$table->string('name');
$table->timestamps();
$table->foreign('admin_id')->references('id')->on('admins');
});
这是针对地区的,我还有另外 3 个与此完全相同的名称,address_thanas
、address_areas
、address_building_names
& address_zips
;
最后一个的唯一例外是 code
而不是其他 table 上的 name
:
Schema::create('address_zips', function (Blueprint $table) {
$table->increments('id');
$table->integer('admin_id')->unsigned();
$table->string('code'); // Look other table has name here........
$table->timestamps();
$table->foreign('admin_id')->references('id')->on('admins');
});
我将构建的地址存储在名为 addresses
Schema::create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->integer('district_id')->unsigned();
$table->integer('thana_id')->unsigned();
$table->integer('area_id')->unsigned();
$table->integer('zip_id')->unsigned();
$table->integer('building_name_id')->nullable()->unsigned();
$table->string('building');
$table->integer('floor');
$table->string('apt')->nullable();
$table->text('comment')->nullable();
$table->timestamps();
$table->foreign('district_id')->references('id')->on('address_districts');
$table->foreign('thana_id')->references('id')->on('address_thanas');
$table->foreign('area_id')->references('id')->on('address_areas');
$table->foreign('zip_id')->references('id')->on('address_zips');
$table->foreign('building_name_id')->references('id')->on('address_building_names');
});
在 Address
模型中,我定义了如下关系:
public function district() {
return $this->belongsTo(AddressDistrict::class, 'district_id');
}
public function thana() {
return $this->belongsTo(AddressThana::class, 'thana_id');
}
public function area() {
return $this->belongsTo(AddressArea::class, 'area_id');
}
public function building_name() {
return $this->belongsTo(AddressBuildingName::class, 'building_name_id');
}
public function zip() {
return $this->belongsTo(AddressZip::class, 'zip_id', 'id');
}
然后当我尝试使用 Address::create($data)
创建一个新地址时
我收到错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select count(*) as aggregate from `address_zips` where `name` = 2)
在这里我们可以看到它正在比较键 name
而不是 id
我注意到这不是报告无法插入数据或类似的东西,它无法计算相关模型并因此中止数据插入
这是为什么? 但奇怪的是我可以通过(手动插入一行到数据库中进行测试以查看是否可以检索数据)检索数据
$addresses = Address::orderByDesc('created_at')->get();
//loop as $address
$address->district->name
$address->zip->code
...
& 这很完美
当我创建新记录时,我需要这样的查询:
select count(*) as aggregate from `address_zips` where `id` = 2
任何帮助将不胜感激。 感谢您阅读这么长的问题。
问题出在您的 $data
数组中。可能您复制了一个带有输入 name
的表单,您需要将其重命名为 code
以便自动保存值。
或者,您可以手动设置输入
$address = new Address();
$address->code = request('name');
$address->save();