如何更新laravel 5中的数组?

时间:2017-09-25 08:24:21

标签: laravel-5

我正在尝试更新数组中的值。 我可以创建,但如果我尝试更新,它只更新最后一个值。 如果我使用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' => $invoice->id,
            'name' => $name[$key],
            'price' => $price[$key],
            'qty' => $qty[$key],
            'total' => $total[$key]
        ]);



     }   

如果我使用save()我会收到此错误

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, array given,

感谢

3 个答案:

答案 0 :(得分:1)

谢谢你的帮助金字塔先生。这是代码:

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




    Session::flash('success', 'Invoice Updated');

    return redirect()->route('invoices');

}

使用这个确切的代码我可以创建并且工作正常但是如果我用它来更新它不会允许我。

数据库

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

}
}

感谢

答案 1 :(得分:0)

我仍然有些疑惑,但我找到了一些我认为可以帮助你的东西。

$invoice->products()->update($request->all());

答案 2 :(得分:0)

   $products = $request->all();


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

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

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

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