0

I am trying to update values from an array. i can create but if I try to update, it updates only last value. if I use save() i get an error. I tried everything i could researching. no success. here is my code.

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



     }   

if I use save() i get this error

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

thanks

6
  • What do you actually want to do or achieve ? Also, the code provided seems to be incomplete and unclear. Please show some more code. Commented Sep 25, 2017 at 8:26
  • There is a problem in the logic here $name is just a string ! or not ? and as the error said ... save() must be an instance of Model array given so you should pass a model to save() not an array !! Commented Sep 25, 2017 at 8:32
  • its clear to what i am trying to achieve. Maraboc, tried to pass a mode to save and didn't work. I know the logic is wrong, just don't know how to fix as I am new in laravel. thanks Commented Sep 25, 2017 at 8:41
  • I can create with this same logic but I cannot update. thanks Commented Sep 25, 2017 at 9:03
  • what is $invoice here? Commented Sep 25, 2017 at 10:49

3 Answers 3

1

thanks for your help Mr. pyramid. here is the code:

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

}

with this exactly code I can create and works fine but if i use to update it won't allow me.

database

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

products

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

relationship

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

}
}

thanks

Sign up to request clarification or add additional context in comments.

Comments

0

I still have some doubts but I found something which I think can help you.

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

1 Comment

ok please add few more code so that I can understand the flow. :) i am confused for $invoice
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]
        ]);
     }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.