【发布时间】: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))
【问题讨论】: