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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46853988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:51:02  来源:igfitidea点击:

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

phpmysqllaravel

提问by Momo

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 S.O 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'. Laravel by default seems to take idby default as my primary key but then it is product_id.

将我的表单数据提交到产品表后,出现错误Column not found: 1054 Unknown column 'id' in 'where clause'。默认情况下,Laravel 似乎id默认作为我的主键,但后来它是product_id.

In my model, i have specified protected $primaryKey = 'product_id'and it wouldn't solve my problem. What am i doing differently ?

在我的模型中,我指定了protected $primaryKey = 'product_id'它并不能解决我的问题。我在做什么不同?

回答by Rusben Guzman

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

回答by Momo

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? Can you show the full Sql error log?

当它说明Unknown column 'id' in 'where clause'但它说明的是产品表吗?你能显示完整的Sql错误日志吗?

I suspect it might be other relationship caused the error.

我怀疑可能是其他关系导致了错误。

回答by Raza Mehdi

If you are using resoure routes, then you need to specify the route key name as well:

如果您使用资源路由,那么您还需要指定路由键名称:

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