2017-09-23 49 views
0

我需要一些帮助插入数据库发票和来自同一表单的产品。我有这么多的错误。如何插入数据库发票和laravel中的产品5.3

发票数据库:

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); 
+1

什么是这么多的错误? –

+0

HasOneOrMany.php中的ErrorException异常第221行: 传递给Illuminate \ Database \ Eloquent \ Relations \ HasOneOrMany :: save()的参数1必须是Illuminate \ Database \ Eloquent \ Model的一个实例,布尔给定,在/ Applications/XAMPP中调用/xamppfiles/htdocs/angie/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php第237行,并定义 –

+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); 
} 
+0

你的传说!谢谢 –

+0

不要忘记接受答案和表决:) –

+0

如果你想防止因错误或其他原因而中断这个过程,你可以使用数据库事务。这将确保插入过程成功或失败(当它失败时,注意将插入数据库) –