2017-09-25 59 views
0

我很努力地找到一個合適的邏輯來更新Laravel中的這個數組。有了這個相同的邏輯,我可以創建,但我嘗試更新它只會更新數組的最後一行。感謝如何更新Laravel 5中的數組?

這裏是代碼

public function update(Request $request, $id) 
{ 

$invoice = Invoice::findOrFail($id); 

$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; 

$invoice->save(); 

$products = $request->all(); 


    $name = $products['name']; 
    $price = $products['price']; 
    $qty = $products['qty']; 
    $total = $products['total']; 

foreach($name as $key => $n) { 

    $invoice->products()->update([ 

     //=> $invoice->id, 
     'name' => $name[$key], 
     'price' => $price[$key], 
     'qty' => $qty[$key], 
     'total' => $total[$key] 
    ]); 
} 

與此完全相同的代碼,我可以創建和工作正常,但如果我嘗試更新只更新數組中的最後一個記錄。

數據庫

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(); 
}); 

關係

class Invoice extends Model { 
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'); 
} 


class Product extends Model 
{ 

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'); 

} 
} 

array:13 [▼ 
"_token" => "RBtz42WyWeebnDTrSkhVhN2XC00f7MhyihI08lvA" 
"invoice_no" => "1234" 
"client" => "Denisson de Souza" 
"title" => "owner" 
"client_address" => "20/590 pine ridge road" 
"invoice_date" => "2017-09-25" 
"due_date" => "2017-09-30" 
"name" => array:4 [▼ 
    0 => "11" 
    1 => "22" 
    2 => "33" 
    3 => "44" 
] 
"price" => array:4 [▼ 
    0 => "32" 
    1 => "32" 
    2 => "32" 
    3 => "32" 
] 
"qty" => array:4 [▼ 
0 => "1" 
1 => "2" 
2 => "3" 
3 => "4" 
] 
"total" => array:4 [▼ 
0 => "32" 
1 => "64" 
2 => "96" 
3 => "128" 
] 
"subtotal" => "93.00" 
"grandtotal" => "106.00" 
] 
+0

請添加一個調用update()到您的文章 –

+0

是什麼$產品看起來像代碼?你可以print_r()或var_dump()嗎? –

+0

產品更新功能是什麼? –

回答

0

像下面的東西應該工作:

添加另一個隱藏字段您的形式:

<input type="hidden" name="id[]" value="{{ $product->id }}"> 

然後用以下內容替換您的foreach循環:

$productObjs = Product::where('invoice_id', $invoice->id)->get(); 
foreach($productObjs as $prod){ 
    $key = array_search($prod->id, $products['id']); 
    $prod->name = $name[$key]; 
    $prod->price = $price[$key]; 
    $prod->qty = $qty[$key]; 
    $prod->total = $total[$key]; 
    $prod->save(); 

} 
+0

感謝我的人,但它不工作,它是仍然更新最後的結果,而不是每個人。 –

+0

嗨我的人,我已經在你的代碼做了一些改變,把箭頭出去=和添加分號然而這段代碼沒有保存任何東西,它仍然是相同的文本,所以不更新。奇怪的。 –

+0

var_dump($ productObjs); –