我需要一些帮助,从同一表格插入数据库发票和产品。我犯了很多错误。
发票数据库:
Schema::create('invoices', function (Blueprint $table) {
$table->increments('id');
$table->integer('invoice_no');
$table->date('invoice_date');
$table->date('due_date');
$table->string('title');
$table->string('client');
$table->string('client_address');
$table->decimal('subtotal');
$table->decimal('grandtotal');
$table->timestamps();
});
产品数据库:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('invoice_id')->unsigned();
$table->string('name');
$table->string('qty');
$table->string('price');
$table->string('total');
$table->timestamps();
});
发票模型:
protected $fillable = [
'client',
'client_address',
'title',
'invoice_no',
'invoice_date',
'due_date',
'discount',
'subtotal',
'grandtotal'
];
产品型号:
public function products() {
return $this->hasMany('App\Product', 'invoice_id');
}
protected $casts = [
'name' => 'array',
'price' => 'array',
'qty' => 'array',
'total' => 'array'
];
protected $fillable = ['invoice_id','price','qty','total','name'];
public function invoice() {
return $this->belongsTo('App\Invoice');
}
发票控制器:
$invoice = new Invoice();
$invoice->invoice_no = $request->invoice_no;
$invoice->client = $request->client;
$invoice->title = $request->title;
$invoice->client_address = $request->client_address;
$invoice->invoice_date = $request->invoice_date;
$invoice->due_date = $request->due_date;
$invoice->subtotal = $request->subtotal;
$invoice->grandtotal = $request->grandtotal;
$input = $request->all();
$product = new Product();
$product->name = $input['name'];
$product->price = $input['price'];
$product->qty = $input['qty'];
$product->total = $input['total'];
$invoice->save();
$product->invoice_id = $invoice->id;
$invoice->products()->save($product);
答案 0 :(得分:0)
您的发票上有很多产品。所以,首先创建发票
$invoice = \Invoice::create([
'invoice_no' => $request->invoice_no,
'client' => $request->client,
'title' => $request->title,
'client_address' => $request->client_address,
'invoice_date' => $request->invoice_date,
'due_date' => $request->due_date,
'subtotal' => $request->subtotal,
'grandtotal' => $request->grandtotal
]);
然后,添加其产品
$product = $invoice->products()->create([
'name' => $request->name,
'price' => $request->price,
'qty' => $request->qty,
'total' => $request->total
])
如果您有多个产品,请将产品创建放在foreach循环中。
如何管理产品的插入,取决于您的表单结构。在接下来,我将举例说明整个过程。
我认为这是你的形式:
<form>
<div class="product">
<input name="products[0][name]" >
<input name="products[0][price]" >
<input name="products[0][qty]" >
<input name="products[0][total]" >
</div>
<div class="product">
<input name="products[1][name]" >
<input name="products[1][price]" >
<input name="products[1][qty]" >
<input name="products[1][total]" >
</div>
<div class="product">
<input name="products[2][name]" >
<input name="products[2][price]" >
<input name="products[2][qty]" >
<input name="products[2][total]" >
</div>
<button type="submit" >Submit</button>
</form>
提交表单后,您将拥有一系列产品:
$request->products
返回
[
[
'name' => 'some name',
'price' => '10',
'qty' => '2',
'total' => '20',
], [
'name' => 'some name',
'price' => '10',
'qty' => '2',
'total' => '20',
], [
'name' => 'some name',
'price' => '10',
'qty' => '2',
'total' => '20',
],
]
现在您可以将它们插入数据库中,如下所示:
foreach($request->products as $product) {
$invoice->products()->create([
'name' => $product['name'],
'price' => $product['price'],
'qty' => $product['qty'],
'total' => $product['total']
]);
}
OR
foreach($request->products as $product) {
$invoice->products()->create($product);
}