【问题标题】:Issue with pivot table Laravel数据透视表 Laravel 的问题
【发布时间】:2018-12-16 07:46:44
【问题描述】:

我对 Laravel 中的数据透视表有疑问, 我制作了customersproducts 表和customer_product 表来连接这两个表,但它不起作用。 下面我添加这个数据透视表

    Schema::create('customer_product', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('customer_id');
        $table->foreign('customer_id')->references('id')->on('customers');
        $table->unsignedInteger('product_id');
        $table->foreign('product_id')->references('id')->on('products');
        $table->decimal('selling_customer_price');
        $table->decimal('purchase_customer_price'); 
        $table->decimal('consumed_customer_price');
        $table->timestamps();
    });
}

我的 ProductController 的一部分,我在其中列出产品并希望向客户展示产品

public function list()
{

    return view('products.list', [
        'customers' => Customer::all(),
        'products' => Product::orderby('name')->get(),

    ]);
}

以及部分简单刀片代码

<div>
     @foreach ($customers as $customer)
        <li> {{ $customer->name }} {{ $customer->products }} </li>
     @endforeach
</div>

产品类的一部分

public function customers()
{
    return $this->belongsToMany(Customer::class);
}

和客户类的一部分

public function products()
{
    return $this->hasMany(Product::class);
}

当我键入 list site 时,我在 product 表中出现了关于缺少 customer_id 列的错误,但我想使用我的数据透视表,因为我必须对不同的客户使用不同的价格。

SQLSTATE[42S22]:未找到列:1054 未知列 “where 子句”中的“products.customer_id”(SQL:select * from products 其中products.customer_id = 1 和 products.customer_id 不为空)(查看: /home/vagrant/code/resources/views/products/list.blade.php)

【问题讨论】:

  • SQL 错误提示您忘记使用 customer_id 列更新产品架构... 表 customer_product 是否正确创建?因为$table-&gt;foreign('customer_id')-&gt;references('id')-&gt;on('customers'); 应该会失败。假设mysql的默认引擎是InnoDB,它只支持外键。
  • customer_id 字段是否存在于您的产品表中。

标签: php mysql laravel migration pivot-table


【解决方案1】:

您说您有多对多关系,那么您应该有如下关系,并且从您的 cmets 中,您在数据透视表中有 selling_customer_price 字段,因为您必须使用 withPivot。详情请查看https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

产品类的一部分

public function customers()
{
    return $this->belongsToMany(Customer::class)->withPivot('selling_customer_price');
}

和客户类的一部分

public function products()
{
    return $this->belongsToMany(Product::class)->withPivot('selling_customer_price');
}

然后像这样获取它

public function list()
{

    return view('products.list', [
        'customers' => Customer::with('products')->all(),
        'products' => Product::orderby('name')->get(),

    ]);
}

在视图中

<div>
      <ul>
        @foreach ($customers as $customer)
          <li> {{ $customer->name }} </li>
          <ul>
            @foreach ($customer->products as $product)
                <li> {{ $product->name }} </li>
                <li> {{ $product->pivot->selling_customer_price }} </li>
            @endforeach
          </ul>
        @endforeach
     </ul>
</div>

【讨论】:

  • 谢谢,我不记得在两个班级都使用过belongsToMany,你的帖子对我很有帮助。
  • 添加您的问题,而不是回答
  • 我还有一个问题,因为它显示了与客户数据透视表相关的产品信息,但我在这个数据透视表中添加了 sell_customer_price。我有疑问是否可以从该单元格获取信息,或者存储客户特价是个坏主意? $table->decimal('sales_customer_price');
  • @azbroja 用selling_customer_price 更新答案检查它
  • 如果它有效,那么您可以接受它作为答案并为未来的访问用户投票
猜你喜欢
  • 2015-09-21
  • 1970-01-01
  • 2019-07-04
  • 2021-03-09
  • 2018-04-10
  • 2021-07-27
  • 2020-08-04
  • 2017-07-03
相关资源
最近更新 更多