2017-09-25 1 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

$ 제품이 생겼 어떤 게시물 –

+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

i-man에게 감사드립니다. 여전히 각각의 결과가 아니라 마지막 결과를 업데이트합니다. –

+0

안녕하세요, 안녕하세요. 저는 코드에 몇 가지 변경 작업을 수행했습니다. 화살표를 =로 가져 와서 세미콜론을 추가했지만이 코드는 아무 것도 저장하지 않고 여전히 동일한 텍스트이므로 업데이트하지 않았습니다. 기묘한. –

+0

var_dump ($ productObjs); –

관련 문제