雄辩地使用错误的键来表示某些表

时间:2017-11-04 18:28:49

标签: laravel eloquent foreign-keys laravel-eloquent laravel-5.5

使用: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_thanasaddress_areasaddress_building_names& address_zips;

最后一个的唯一例外是在其他表上有code而不是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

任何帮助都将受到高度赞赏。 感谢您阅读这个长期的问题。

1 个答案:

答案 0 :(得分:0)

问题出在您的$data数组中。您可能复制了带有输入name的表单,您需要将其重命名为code才能自动保存值。

或者,您可以手动设置输入

$address = new Address();
$address->code = request('name');
$address->save();