1

I am new in Laraver, I am using Eloqunet model... deleteItem function, delete item with offer_in and nbr...When delete item I want to add a new nbr from each item with equal offer_id, starting from 1. I need to define counter = 1, and increase by 1 for every item.. I don't know how to write a for loop, or foreach loop, which pass through selected item, and change 'nbr' with the corresponding value of the counter?

my code is:

public function deleteItem(Request $request, $offer_id, $nbr) {
    $item = OfferItem::select('id', 'offer_id', 'nbr', 'product', 'quantity', 'item_price', 'item_tax', 'item_total')
                     ->where('offer_id', $offer_id)
                     ->where('nbr', $nbr)
                     ->first();
    $item->delete();
        
    //select new items for offer, after deleting item
    $items = OfferItem::select('id', 'offer_id', 'nbr', 'product', 'quantity', 'item_price', 'item_tax', 'item_total')
                      ->where('offer_id', $offer_id)
                      ->get();

    //todo

    return response()->json($item);
}
4
  • what do you mean by coresponding value of counter? Commented Jul 28, 2020 at 11:54
  • for example, i have 5 items for offer, nbr are 1,2,3,4,5. when I delete item with nbr 3 I want to ''reset'' nbr to be again 1,2,3,4.. I don'w want to have items for offer with nbr 1,2,4,5 when I delete item with nbr3 Commented Jul 28, 2020 at 11:58
  • This is still not very clear to me. What is nbr? What does its schema look like? How exactly is it related to OfferItem? Commented Jul 28, 2020 at 12:00
  • hm, nbr is number of items..first item in list has nbr 1, second item in list has nbr 2... Commented Jul 28, 2020 at 12:06

2 Answers 2

2

Using the Laravel collections, you can just loop the collection and set new count to nbr. Using $count as a reference for the closure, to make the logic work.

$int = 1;

$items->each(function (OfferItem $item) use (&$count) {
    $item->nbr = $count;
    $item->save();

    $count++;
});
Sign up to request clarification or add additional context in comments.

Comments

2

You can achieve this purely in SQL like in these examples.

You can achieve what you want using Laravel by:

public function deleteItem(Request $request, $offer_id, $nbr) {
    OfferItem::query()
             ->where('offer_id', $offer_id)
             ->where('nbr', $nbr)
             ->delete();
        
    // Select new items for offer, after deleting item
    $items = OfferItem::where('offer_id', $offer_id)
                      ->where('nbr', '>', $nbr)
                      ->get()
                      ->sortBy('nbr')
                      ->map(function ($item) use ($nbr) {
                          $update = [
                              'id' => $item->id,
                              'nbr' => $nbr,
                          ];
                          
                          $nbr++;

                          return $update;
                      });

    OfferItem::query()->update($items);

    return response()->json($item); // It is not clear from your question what you
                                    // are trying to return here. Why would you
                                    // return a deleted item?
}

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.