Laravel 旋转 table

Laravel Pivot table

所以我正在尝试为 "order" 和 "item" 做一个支点 table。但我得到一个 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');
    });
}

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

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

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

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

通常主键就是 id,所以 Laravel 查找的键是 order_id.