我正在使用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))
答案 0 :(得分:2)
我相信您的product_quantity字段必须为可为空或具有默认值。我会说,在您的表定义中product_quantity看起来像是以下两者之一:
$table->integer('product_quantity')->nullable();
要么
$table->integer('product_quantity')->default(0);
最好在像恕我直言这样的问题中共享表定义。