Я использую 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_id & product_id & product_quantity в моей миграции таблицы invoice_product, и вот ошибка, которую я получаю, когда хочу сохранить свою форму:
Поле 'product_quantity' не имеет значения по умолчанию (SQL: вставить в invoice_product
(invoice_id
, product_id
) значения (22, 1))