0

I have a edit form which edit an array like (Invoice) , I want when i edit the Invoice_no a can also able to add the new item on the same invoice number

Example the Inv_no=300 and its carry 3 arrays ([... .... ... ]), so I want to add the new one array to be 4 arrays.

On my code below is update array well data but if i add new one row does not save that new one while it keep update,

what can i do here to add/save the new one data.

public function update(Request $request, $inv_no)
 {
  $data = $request->all();
//dd($data);

   $stocks = Stock::where('inv_no', $inv_no)->get();
  $i = 0;

foreach ($stocks as $stock) {  
    Stock::where('inv_no', $inv_no)
        ->where('id', $stock->id)
        ->update([
            'pid' => $request->pid[$i],
            'qty' => $request->qty[$i],
            'inv_no' => $request->inv_no,
            'user_id' => Auth::user()->id,
            'Indate'=>$request->Indate,
            'supplierName' => $request->supplierName,
            'receiptNumber' => $request->receiptNumber,
            'truckNumber' => $request->truckNumber,
            'driverName' => $request->driverName,
            'remark' => $request->remark,
        ]);

        $i++;
    }

   return $this->index();
  }
2
  • have you tried updateOrCreate()? Commented May 18, 2020 at 14:29
  • am tried but not works as need @djunehor Commented May 18, 2020 at 14:53

4 Answers 4

2

I think you need something like that. You have mentioned that you need to update if the invoice number exists and you also want to a new item with this invoice number. If you have any query, feel free to ask me

public function update(Request $request, $inv_no)
{
    $data = $request->all();
    $stocks = Stock::where('inv_no', $inv_no)->get();
    $i = 0;
    foreach ($stocks as $stock) {
        $stock->update([
            'pid' => $request->pid[$i],
            'qty' => $request->qty[$i],
            'user_id' => Auth::user()->id,
            'Indate'=>$request->Indate,
            'supplierName' => $request->supplierName,
            'receiptNumber' => $request->receiptNumber,
            'truckNumber' => $request->truckNumber,
            'driverName' => $request->driverName,
            'remark' => $request->remark,
        ]);
        $i++;
    }

    $totalPid = count($request->pid);
    $totalQty= count($request->qty);
    Stock::create([
        'pid' => $request->pid[$totalPid - 1],
        'qty' => $request->qty[$totalQty - 1],
        'inv_no' => $request->inv_no,
        'user_id' => Auth::user()->id,
        'Indate'=>$request->Indate,
        'supplierName' => $request->supplierName,
        'receiptNumber' => $request->receiptNumber,
        'truckNumber' => $request->truckNumber,
        'driverName' => $request->driverName,
        'remark' => $request->remark,
    ]);

    return $this->index();
}
Sign up to request clarification or add additional context in comments.

9 Comments

THANK YOU BROTHER, Its works perfect update and add new items
ok, but one qns, why am get Undefined offset: 3 when I update again, and it point Stock::create([ 'pid' => $request->pid[$i],
When you are updating again, you are not updating your request. Please show me your requested pid and qty
Maybe you are sending array but don't know why. Please explain it to me
Will be your $request->pid and $request->qty separate for each entry? Share your request with me, otherwise, how I help you?
|
0

You may use updateOrCreate method:

foreach ($stocks as $stock) {  
    Stock::updateOrCreate(['id' => $stock->id], [
       'pid' => $request->pid[$i],
       'qty' => $request->qty[$i],
       'inv_no' => $request->inv_no,
       'user_id' => Auth::user()->id,
       'Indate'=>$request->Indate,
       'supplierName' => $request->supplierName,
       'receiptNumber' => $request->receiptNumber,
       'truckNumber' => $request->truckNumber,
       'driverName' => $request->driverName,
       'remark' => $request->remark,
    ]);
}

See Laravel docs for more info.

1 Comment

Bother, tested , it update the same data which i want to edit, but not create additional one which i entered
0

I can see now what you need. If you put this where(['id', $stock->id]) you will only find and update what already exists. Since your comparing with the values you searched doing $stocks = Stock::where('inv_no', $inv_no)->get();.

If you need to search where 'inv_no' is the value you inputed or create a new record, do:

Stock::updateOrCreate(['inv_no', $inv_no], [
   'pid' => $request->pid[$i],
   'qty' => $request->qty[$i],
   'inv_no' => $request->inv_no,
   'user_id' => Auth::user()->id,
   'Indate'=>$request->Indate,
   'supplierName' => $request->supplierName,
   'receiptNumber' => $request->receiptNumber,
   'truckNumber' => $request->truckNumber,
   'driverName' => $request->driverName,
   'remark' => $request->remark,
]);

That way you are going to search any records with 'inv_no' equals the value you entered, update it or create a new value if it doesn't exists. Also, you can remove the lines $stocks = Stock::where('inv_no', $inv_no)->get(); and foreach ($stocks as $stock) {}.

4 Comments

i get this error, SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from loadings` where (0 = order_no and 1 = ONP1805200001) limit 1) `
Sorry, I wrote it wrong. It should be ['inv_no' => $inv_no] instead of ['inv_no', $inv_no]
Let me know if it solved your issue. Notice it will only create a new record if there isn't any records, that is, if there's no Stock with 'inv_no' = $inv_no.
it update only that data are existing, but the issue here is to create the new product_id which i want to added on this $inv_no suppose i have product_id on this inv_no then i want to add the new product_id on the same inv_no
0

UpdateorCreate is what you need but you must twist it so it works for your needs.

UpdateOrCreate can take 2 array parameters. The 1st array is basically your where clause telling the function to look for the specific parameters and if it finds them to update otherwise create a new record.

 Stock::updateOrCreate([
     'id' => $stock->id
     'pid' => $request->pid[$i],
     'inv_no' => $request->inv_no], 
     'qty' => $request->qty[$i],
   [
       'user_id' => Auth::user()->id,
       'Indate'=>$request->Indate,
       'supplierName' => $request->supplierName,
       'receiptNumber' => $request->receiptNumber,
       'truckNumber' => $request->truckNumber,
       'driverName' => $request->driverName,
       'remark' => $request->remark,
    ]);

Not clear about your data but the above code will check in your database if there is a record with:

  • id matching $stock->id
  • pid matching $request->pid[$i]
  • inv_no matching $request->inv_no

You can extend your where clause even further, you can even use only 1 array with all your data and tell the function to look for a record which has all the fields matching otherwise create a new record.

2 Comments

brother, your logic is good and tested its update the matching records only but not add the new data of request->pid and qty,
Updated my answer. If it still not working that means that your counter is not working and ` $request->pid[$i]` and qty return always the same value. Try to debug it and echo your values.

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.