2017-09-23 45 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

如果你想防止因錯誤或其他原因而中斷這個過程,你可以使用數據庫事務。這將確保插入過程成功或失敗(當它失敗時,注意將插入數據庫) –