简体   繁体   English

Laravel数据透视表

[英]Laravel Pivot table

So im trying to do a pivot table for "order" and "item". 因此,我试图为“订单”和“项目”创建数据透视表。 but im getting a SQL error 但我收到一个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))

the only thing i can notice off the bat is "order_order_id" should be "order_id" and "item_item_id" "item_id" but im not sure. 我唯一能注意的是“ order_order_id”应为“ order_id”和“ item_item_id”为“ item_id”,但我不确定。

i get this error when calling 我在致电时收到此错误

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

Here is my models 这是我的模特

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');
}

}

Here is my migrations and foreign key constraints 这是我的迁移和外键约束

Order: 订购:

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);
    });
   }

Item: 项目:

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);
    });
}

Orderline: 订单行:

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: FkConstraintToOrder:

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

FkConstraintToOrderline: 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');
    });
}

I think I'm missing something but I'm not sure what. 我想我缺少了一些东西,但我不确定。 Any help would be appreciated. 任何帮助,将不胜感激。

While you can name your pivot table whatever you want, according to the official Laravel documentation at https://laravel.com/docs/5.5/eloquent-relationships you should name it based on the two tables it is connecting in alphabetical order, in this case item_order. 尽管可以随意命名数据透视表,但根据https://laravel.com/docs/5.5/eloquent-relationships上的 Laravel官方文档,您应该根据它按字母顺序连接的两个表来命名它,这种情况下为item_order。 Doing so will make your code simpler and easier to read. 这样做将使您的代码更容易阅读。

As to your question, the way Laravel determines the name of the columns in the pivot table is based on the primary key of the two tables in question and prepends the name of the class to the front, so since your primary key is order_id because of protected $primaryKey = "order_id"; 至于您的问题,Laravel确定数据透视表中列的名称的方式是基于所讨论的两个表的主键,并将类的名称放在前面,因此由于您的主键是order_id,因此protected $primaryKey = "order_id"; it takes that and adds the name of the class, so it calls it order_order_id. 它接受并添加类的名称,因此将其称为order_order_id。 You can override this functionality by doing something like this: 您可以通过执行以下操作来覆盖此功能:

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

Usually the primary key is just id , so the key that Laravel would look for would be order_id . 通常,主键只是id ,所以Laravel寻找的键是order_id

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM