Laravel Pivot表

时间:2017-10-30 23:28:10

标签: php laravel orm laravel-eloquent

所以我试图为"命令"做一个数据透视表。和"项目"。但我得到一个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))

我唯一能注意到的是#34; 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');
    });
}

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

1 个答案:

答案 0 :(得分:0)

虽然您可以根据需要命名您的数据透视表,但根据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