【问题标题】:Laravel Pivot tableLaravel 数据透视表
【发布时间】:2017-10-30 23:28:10
【问题描述】:

所以我试图为“订单”和“项目”做一个数据透视表。但我得到一个 SQL 错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'orderline.order_order_id' in 'field list' (SQL: select `item`.*, `orderline`.`order_order_id` as `pivot_order_order_id`, `orderline`.`item_item_id` as `pivot_item_item_id`, `orderline`.`quantity` as `pivot_quantity` from `item` inner join `orderline` on `item`.`item_id` = `orderline`.`item_item_id` where `orderline`.`order_order_id` in (1, 2, 3, 4, 5))

我唯一能注意到的是“order_order_id”应该是“order_id”和“item_item_id”“item_id”,但我不确定。

我在调用时收到此错误

$customers = Customer::with('orders.items')->get();
dd($customers);

这是我的模型

class Customer extends Model
{

public $timestamps = false;
protected $table = "customer";
protected $primaryKey = "customer_id";

public function orders(){
    return $this->hasMany('App\Order','customer_id');
}
}

class Order extends Model
{
/**
 * @var bool
 */
public $timestamps = false;
protected $table = "order";
protected $primaryKey = "order_id";

public function customer(){
    return $this->hasOne('App\Customer','customer_id');
}

public function items(){
    return $this->belongsToMany('App\Item','orderline')->withPivot('quantity');
}

}


class Item extends Model
{
/**
 * @var bool
 */
public $timestamps = false;

protected $table = "item";
protected $primaryKey = "item_id";

public function orders(){
    return $this->belongsToMany('App\Order','orderline')->withPivot('quantity');
}

public function stock(){
    return $this->hasOne('App\Stock','item_id');
}

}

这是我的迁移和外键约束

订单:

public function up()
{
    Schema::create('order', function (Blueprint $table) {
        $table->increments('order_id');
        $table->integer('customer_id')->unsigned();
        $table->date('date_placed');
        $table->date('date_shipped');
        $table->decimal('shipping_charge',7,2);
    });
   }

项目:

public function up()
{
    Schema::create('item', function (Blueprint $table) {
        $table->increments('item_id');
        $table->string('description',64);
        $table->decimal('cost_price',7,2);
        $table->decimal('sell_price',7,2);
    });
}

订单线:

public function up()
{
    Schema::create('orderline', function (Blueprint $table) {
        $table->integer('order_id')->unsigned();
        $table->integer('item_id')->unsigned();
        $table->integer('quantity');
        $table->primary(['order_id','item_id']);
    });
}

FkConstraintToOrder:

public function up()
{
    Schema::table('order', function (Blueprint $table) {
        $table->foreign('customer_id')->references('customer_id')->on('customer')->onDelete('cascade');
    });
}

FkConstraintToOrderline:

public function up()
{
    Schema::table('orderline', function (Blueprint $table) {
        $table->foreign('order_id')->references('order_id')->on('order')->onDelete('cascade');
        $table->foreign('item_id')->references('item_id')->on('item')->onDelete('cascade');
    });
}

我想我错过了一些东西,但我不确定是什么。任何帮助将不胜感激。

【问题讨论】:

  • 请也提供您的客户型号。
  • @manniL 已更新

标签: php laravel orm laravel-eloquent


【解决方案1】:

虽然您可以随意命名数据透视表,但根据https://laravel.com/docs/5.5/eloquent-relationships 的 Laravel 官方文档,您应该根据它按字母顺序连接的两个表来命名它,在本例中为 item_order。这样做将使您的代码更简单、更易于阅读。

关于您的问题,Laravel 确定数据透视表中列名称的方式是基于所讨论的两个表的主键并将类的名称放在前面,所以由于您的主键是order_id 因为protected $primaryKey = "order_id"; 它接受了它并添加了类的名称,所以它称之为order_order_id。您可以通过执行以下操作来覆盖此功能:

return $this->belongsToMany('App\Item', 'orderline', 'order_id', 'item_id');

通常主键只是 id,所以 Laravel 会寻找的键是 order_id

【讨论】:

  • 谢谢,做覆盖修复它,它总是小事。
猜你喜欢
  • 2018-04-10
  • 2017-12-15
  • 2021-08-17
  • 1970-01-01
  • 2015-09-10
  • 2014-08-24
  • 2019-07-09
  • 2016-12-06
  • 2016-12-26
相关资源
最近更新 更多