简体   繁体   English

找不到列:1054“ where子句”中的未知列“ id”-Laravel

[英]Column not found: 1054 Unknown column 'id' in 'where clause' - Laravel

There has been lot of issues on this but i am following the exact procedure to solve this issue as described in the other related SO questions yet i get the same error. 对此有很多问题,但是我正在按照其他相关SO问题中所述的确切方法来解决此问题,但我遇到了同样的错误。

Shop

public function products()
    {
        return $this->hasMany('App\Product');
        //->withTimestamps();
    }

Product 产品

public function shop()
    {
        return $this->belongsTo('App\Shop');    
        //->withTimestamps();
    }

This is how my schema looks like 这就是我的架构的样子

    Schema::create('products', function (Blueprint $table) {
                $table->increments('product_id');
                $table->integer('shop_id')->unsigned()->nullable();
                $table->foreign('shop_id')->references('id')->on('shops');

                 $table->timestamps();
);

Controller 调节器

$products = new Product(array(
            'name' => $request->('name');
        ));

        $shop->products()->save($products);

After submitting my form data into the products table, i get an error Column not found: 1054 Unknown column 'id' in 'where clause' . 将表单数据提交到产品表后,出现错误Column not found: 1054 Unknown column 'id' in 'where clause' Laravel by default seems to take id by default as my primary key but then it is product_id . Laravel默认似乎默认将id作为我的主键,但是它是product_id

In my model, i have specified protected $primaryKey = 'product_id' and it wouldn't solve my problem. 在我的模型中,我指定了protected $primaryKey = 'product_id' ,它不能解决我的问题。 What am i doing differently ? 我在做什么不同?

In the relationships you have to specify the name of the primary key when it is not called id, try to change the model like this: 在关系中,您必须指定不称为id的主键的名称,尝试像这样更改模型:

SHOP

public function products()
    {
        return $this->hasMany('App\Product', 'product_id');
        //->withTimestamps();
    }

PRODUCT 产品

public function shop()
    {
        return $this->belongsTo('App\Shop', 'product_id');    
        //->withTimestamps();
    }

In the documentation explains how it works: https://laravel.com/docs/5.5/eloquent-relationships#one-to-many 在文档中解释了它是如何工作的: https : //laravel.com/docs/5.5/eloquent-relationships#one-to-many

If you are using resoure routes, then you need to specify the route key name as well: 如果使用的是资源路由,则还需要指定路由键名称:

public function getRouteKeyName()
{
    return 'product_id';
}

I had tried with my local with following: 我曾尝试通过以下方式与本地人联系:

Schema: 架构:

Schema::create('shops', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('products', function (Blueprint $table) {
    $table->increments('product_id');
    $table->string('name');
    $table->integer('shop_id')->unsigned()->nullable();
    $table->foreign('shop_id')->references('id')->on('shops');
    $table->timestamps();
});

Model relationship exactly same as your post. 模型关系与您的帖子完全相同。

Controller code: 控制器代码:

$shop = new Shop;
$shop->name = 'test';
$shop->save();

$products = new Product;
$products->name = 'test';

$shop->products()->save($products);

And final result? 最终结果是什么? It is saved into products table without error. 它被正确地保存到产品表中。 I am using Laravel 5.4 at my local. 我在本地使用Laravel 5.4。

When it stated Unknown column 'id' in 'where clause' but does it stated is products table? 当它Unknown column 'id' in 'where clause'表示“ Unknown column 'id' in 'where clause'是否表示产品表? Can you show the full Sql error log? 您可以显示完整的Sql错误日志吗?

I suspect it might be other relationship caused the error. 我怀疑这可能是其他关系导致的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到列:1054 'where 子句'中的未知列'id' Laravel 5.6 - Column not found: 1054 Unknown column 'id' in 'where clause' Laravel 5.6 未找到列:1054“ where子句”中的未知列“ id” - Column not found: 1054 Unknown column 'id' in 'where clause' Laravel Eloquent Update Column not found: 1054 Unknown column 'id' in 'where Clause' - Laravel Eloquent Update Column not found: 1054 Unknown column 'id' in 'where clause' 无法更新Laravel4中的记录:SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ id” - Unable to update record in Laravel4: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘id’ in ‘where clause' SQLSTATE [42S22]:找不到列:1054“ where子句” Id中的未知列“ id”为空 - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' Id is null 1054 where子句中的未知列? - 1054 Unknown column in where clause? updateOrCreate()给出未找到的列:1054“ where子句”中的未知列“ 0” - updateOrCreate() gives Column not found: 1054 Unknown column '0' in 'where clause' 找不到列:1054'on子句中的未知列'locations.id' - Column not found: 1054 Unknown column 'locations.id' in 'on clause laravel找不到列:1054'order子句'中的未知列'created_at' - laravel Column not found: 1054 Unknown column 'created_at' in 'order clause' SQLSTATE [42S22]:未找到列:1054 laravel 中“where 子句”中的未知列“3” - SQLSTATE[42S22]: Column not found: 1054 Unknown column '3' in 'where clause' in laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM