Php 透视表Laravel的问题

Php 透视表Laravel的问题,php,mysql,laravel,migration,pivot-table,Php,Mysql,Laravel,Migration,Pivot Table,我对Laravel的透视表有问题, 我制作了customers和products表和customer\u product表来连接这两者,但它不起作用。 下面我添加这个透视表 Schema::create('customer_product', function (Blueprint $table) { $table->increments('id'); $table->unsignedInteger('customer_id');

我对Laravel的透视表有问题, 我制作了
customers
products
表和
customer\u 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 products()
{
    return $this->hasMany(Product::class);
}
public function products()
{
    return $this->belongsToMany(Product::class)->withPivot('selling_customer_price');
}
当我键入list site时,我在
product
表中缺少
customer\u id
列时出错,但我想使用我的pivot表,因为我必须对不同的客户使用不同的价格

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


您说过您有多对多关系,那么您应该有下面这样的关系,从您的评论中,您在透视表中有
salling\u customer\u price
字段,您必须使用
with pivot
。详情请查收

产品类别的一部分

public function customers()
{
    return $this->belongsToMany(Customer::class);
}
public function customers()
{
    return $this->belongsToMany(Customer::class)->withPivot('selling_customer_price');
}
和部分客户类别

public function products()
{
    return $this->hasMany(Product::class);
}
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>

    @foreach($customers作为$customer)
  • {{$customer->name}
    • @foreach($customer->products as$product)
    • {{$product->name}
    • {{$product->pivot->销售客户价格}
    • @endforeach
    @endforeach

SQL错误提示您忘记使用客户id列更新产品架构。。。表
customer\u product
是否正确创建?因为
$table->foreign('customer_id')->引用('id')->on('customers')应该失败。。假设mysql的默认引擎是InnoDB,它只支持外键。您的products表中是否存在customer_id字段。谢谢,我不记得在这两个类中都使用了BelongToMany,您的帖子对我很有帮助。添加您的问题,不在回答中我还有一个问题,因为这显示了与客户数据透视表相关的产品信息,但我在此数据透视表中添加了销售价格。我有一个问题,是可以从这个手机获取信息,还是存储客户特价是个坏主意$表->十进制(‘销售价格’)@azbroja在pivot check ITI中用
销售客户价格
更新了答案,如果它有效,那么您可以接受它作为答案,并向未来的访问用户投票