【问题标题】:inserting data into pivot table with pivot error laravel 5.6将数据插入数据透视表中,数据透视错误 laravel 5.6
【发布时间】:2018-06-30 06:29:37
【问题描述】:

我正在使用belongstomany 关系和数据透视表,我可以轻松保存产品,但是当我想用它保存产品数量时返回错误,这是我的代码 发票样板:

class Invoice extends Model
{
    public function products()
    {
        return $this->belongsToMany('App\Product', 'invoice_product', 'invoice_id')
            ->withPivot('product_quantity')
            ->as('invoice_products_pivot');

    }
}

和我的发票控制器创建部分:

public function store(Request $request)
{
    //Validate
    $request->validate([
        'title' => 'required|min:3',
        'description' => 'required',
    ]);

    $invoices = Invoice::create([
        'title' => $request->title,
        'description' => $request->description,
        'client_id' => $request->client_id,

    ]);
    $product_id1 = $request->input('product_id1');
    $product_id2 = $request->has('product_id2');
    $product_id3 = $request->has('product_id3');
    $product_id4 = $request->has('product_id4');
    $product_id5 = $request->has('product_id5');
    $invoices->products()->attach($product_id1);
    $invoices->products()->attach($product_id2);
    $invoices->products()->attach($product_id3);
    $invoices->products()->attach($product_id4);
    $invoices->products()->attach($product_id5);
    return redirect('admin/invoices/' . $invoices->id);
}

这里的观点很抱歉太长了

        <form method="post" action="{{url('admin/invoices')}}">
                {{csrf_field()}}
                <div class="form-group"> <!-- Street 1 -->
                    <label for="title" class="control-label">نام</label>
                    <input type="text" class="form-control" id="title" name="title"
                           placeholder="Street address, P.O. box, company name, c/o">
                </div>

                <div class="form-group">
                    <label>انتخاب محصول</label>
                    <select class="select-menu2-color select2-hidden-accessible" name="product_id1"
                            tabindex="-1"
                            aria-hidden="true">

                        @foreach($produts as $product)
                            <option value="{{$product->id}}">{{$product->name}}</option>
                        @endforeach
                    </select>
                    <div class="form-group"> <!-- Street 1 -->
                        <label for="title" class="control-label">تعداد</label>
                        <input type="text" class="form-control" id="title" name="product_qa1"
                               placeholder="Street address, P.O. box, company name, c/o">
                    </div>
                    <label>انتخاب محصول</label>
                    <select  class="select-menu2-color select2-hidden-accessible" name="product_id2"
                            tabindex="-1"
                            aria-hidden="true">
                        <option value="">لطفا یک محصول انتخاب کنید</option>
                        @foreach($produts as $product)

                            <option value="{{$product->id}}">{{$product->name}}</option>
                        @endforeach
                    </select>
                    <label>انتخاب محصول</label>
                    <select class="select-menu2-color select2-hidden-accessible" name="product_id3"
                            tabindex="-1"
                            aria-hidden="true">
                        <option value="">لطفا یک محصول انتخاب کنید</option>
                        @foreach($produts as $product)
                            <option value="{{$product->id}}">{{$product->name}}</option>
                        @endforeach
                    </select>
                    <label>product1</label>
                    <select class="select-menu2-color select2-hidden-accessible" name="product_id4"
                            tabindex="-1"
                            aria-hidden="true">
                        <option value="">لطفا یک محصول انتخاب کنید</option>
                        @foreach($produts as $product)
                            <option value="{{$product->id}}">{{$product->name}}</option>
                        @endforeach
                    </select>
                    <label>انتخاب محصول</label>
                    <select  class="select-menu2-color select2-hidden-accessible" name="product_id5"
                            tabindex="-1"
                            aria-hidden="true">
                        <option value="">لطفا یک محصول انتخاب کنید</option>
                        @foreach($produts as $product)
                            <option value="{{$product->id}}">{{$product->name}}</option>
                        @endforeach
                    </select>

                </div>
                <div class="form-group">
                    <label class="display-block">انتخاب مشتری</label>
                    <select class="select-custom-colors select2-hidden-accessible" name="client_id" tabindex="-1"
                            aria-hidden="true">
                        @foreach($clients as $client)
                            <option value="{{$client->id}}">{{$client->title}}</option>
                        @endforeach
                    </select>


                </div>
                <div class="form-group"> <!-- Full Name -->
                    <label for="description" class="control-label">توضیحات</label>
                    <input type="text" class="form-control" id="description" name="description"
                           placeholder="John Deer">
                </div>
                <button type="submit" class="btn btn-primary btn-raised legitRipple">ارسال</button>
            </form>

我在 invoice_product 表的迁移中有 invoice_id & product_id & product_quantity,这是我想保存表单时遇到的错误:

字段 'product_quantity' 没有默认值(SQL:插入到 invoice_product (invoice_id, product_id) 值 (22, 1))

【问题讨论】:

    标签: php laravel migration


    【解决方案1】:

    我相信您的 product_quantity 字段必须可以为空或具有默认值。我会说在您的表定义中 product_quantity 看起来像两者之一:

    $table-&gt;integer('product_quantity')-&gt;nullable(); 或者 $table-&gt;integer('product_quantity')-&gt;default(0);

    在像这个恕我直言的问题中分享您的表定义总是更好。

    【讨论】:

      猜你喜欢
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 2018-04-10
      • 1970-01-01
      • 2014-01-27
      • 2018-06-15
      相关资源
      最近更新 更多