ene's avatar
Level 2

unable to update product in foreach loop

so i wanted to update some of my product inside foreach loop, i noticed the form is only updating one of the product but the rest not updating here is my code below

what am i doing wrong here

  @foreach ($products as $product)
        <div class="bg-white hover:shadow rounded-xl mb-3 p-2">
            <div class="">
                <div class="flex justify-between items-center">
                  <div class="flex gap-2">
                    <div class="h-10 w-10 md:w-16 md:h-16">
                        @auth
                        <img class=" rounded-lg object-cover" src="{{ $product->user->profile_photo_url }}"
                        alt="{{ $product->user->account->store_name ?? "" }}" />
                        @endauth
                    </div>
                    <div class="flex flex-col">
                        <h2 class="text-gray-600 font-bold flex items-center gap-1">
                            Kikis Store
                            <svg class="w-4 h-4 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4M7.835 4.697a3.42 3.42 0 001.946-.806 3.42 3.42 0 014.438 0 3.42 3.42 0 001.946.806 3.42 3.42 0 013.138 3.138 3.42 3.42 0 00.806 1.946 3.42 3.42 0 010 4.438 3.42 3.42 0 00-.806 1.946 3.42 3.42 0 01-3.138 3.138 3.42 3.42 0 00-1.946.806 3.42 3.42 0 01-4.438 0 3.42 3.42 0 00-1.946-.806 3.42 3.42 0 01-3.138-3.138 3.42 3.42 0 00-.806-1.946 3.42 3.42 0 010-4.438 3.42 3.42 0 00.806-1.946 3.42 3.42 0 013.138-3.138z"></path></svg>
                        </h2>
                       <div class="text-xs text-gray-500"> {{ $product->created_at->diffForHumans() }}</div>
                    </div>
                    </div>
                    <div class="flex gap-2 items-center -mt-1">
                        <div class="">
                            <span class="tex-xs text-green-500 border border-green-400 rounded-xl px-3 -py-1 md:p-1">₦{{ $product->price }}</span>
                        </div>
                        <x-dropdown>
                            <x-dropdown.header>
                                <x-dropdown.item icon="cog" label="Preferences" />
                                <label for="my-modal-4" class="flex ml-3 gap-2 text-sm modal-button"><svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M13 7h8m0 0v8m0-8l-8 8-4-4-6 6" />
                                  </svg> Boost product</label>
                                <x-dropdown.item separator icon="user" label="My Profile" />
                            </x-dropdown.header>

                            <x-dropdown.item separator label="Help Center" />
                            <x-dropdown.item label="Live Chat" />
                            <x-dropdown.item label="Logout" />
                        </x-dropdown>
                    </div>
                </div>
                <div class="p-2">
                    <p class="text-sm">
                        <a href="{{ route('product.show',$product->slug) }}" class="">{{ $product->title }}</a>
                    </p>
                    <img src="" alt="" class="">
                </div>
                <div class="flex justify-between items-center">
                   @auth
                   <span class="flex gap-1">
                    <button wire:click="like({{ $product->id }})" class="">
                      @if (auth()->user()->hasLiked($product))
                      <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6 text-red-500" viewBox="0 0 20 20" fill="currentColor">
                          <path fill-rule="evenodd" d="M3.172 5.172a4 4 0 015.656 0L10 6.343l1.172-1.171a4 4 0 115.656 5.656L10 17.657l-6.828-6.829a4 4 0 010-5.656z" clip-rule="evenodd" />
                        </svg>
                      @else
                      <svg class="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4.318 6.318a4.5 4.5 0 000 6.364L12 20.364l7.682-7.682a4.5 4.5 0 00-6.364-6.364L12 7.636l-1.318-1.318a4.5 4.5 0 00-6.364 0z"></path></svg>
                      @endif
                    </button>
                    <span class="">
                      {{ $product->likers()->count(); }}
                    </span>
                  </span>
                   @endauth
                    <span class=""><svg class="w-6 h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"></path></svg></span>
                    <span class="text-xs px-2 pb-1 text-center rounded-lg  bg-{{ $product->category->class }}-400">{{ $product->category->name ?? "" }}</span>
                </div>
            </div>
        </div>


        <div>
            <input type="checkbox" id="my-modal-4" class="modal-toggle" />
            <label for="my-modal-4" class="modal cursor-pointer">
                <label class="modal-box relative" for="">
                    <div class="">
                        <form wire:submit.prevent="boosted({{ $product->id }})" class="space-y-5" action="">

                            <ul class="flex flex-col space-y-4 text-center">

                                <li class="w-full">
                                    <input class="peer sr-only" wire:model.defer="amounts" type="radio" value="2000"
                                        id="150" />
                                    <label for="150"
                                        class="inline-block w-full peer-checked:border-transparent px-4 py-3  border border-gray-300 rounded-lg cursor-pointer focus:outline-none peer-checked:ring-green-500 peer-checked:ring-2  peer-checked: ">
                                        <div class="flex flex-col">
                                            <span class="font-bold">N2000</span>
                                          <span class="text-xs">
                                            40 advertisers + 500 views + 2days share
                                            </span>
                                        </div>
                                    </label>

                                </li>
                                <li class="">
                                    <input class="peer sr-only" wire:model.defer="amounts" type="radio" value="4000"
                                        id="200" />
                                    <label for="200"
                                        class="inline-block w-full peer-checked:border-transparent px-4 py-3  border border-gray-300 rounded-lg cursor-pointer focus:outline-none peer-checked:ring-green-500 peer-checked:ring-2  peer-checked: ">
                                        <div class="flex flex-col">
                                            <span class="font-bold">N4000</span>
                                          <span class="text-xs">
                                            80 advertisers + 800 views + 4days share
                                            </span>
                                        </div>
                                    </label>

                                </li>
                                <li class="">
                                    <input class="peer sr-only" wire:model.defer="amounts" type="radio" value="6000"
                                        id="250" />
                                    <label for="250"
                                        class="inline-block w-full peer-checked:border-transparent px-4 py-3  border border-gray-300 rounded-lg cursor-pointer focus:outline-none peer-checked:ring-green-500 peer-checked:ring-2  peer-checked: ">
                                        <div class="flex flex-col">
                                            <span class="font-bold">N6000</span>
                                          <span class="text-xs">
                                            120 advertisers + 1200 views + 6days share
                                            </span>
                                        </div>
                                    </label>

                                </li>
                                <li class="">

                                    <input class="peer sr-only" wire:model.defer="amounts" type="radio" value="8000"
                                        id="300" />
                                    <label for="300"
                                        class="inline-block w-full peer-checked:border-transparent px-4 py-3  border border-gray-300 rounded-lg cursor-pointer focus:outline-none peer-checked:ring-green-500 peer-checked:ring-2  peer-checked: ">
                                        <div class="flex flex-col">
                                            <span class="font-bold">N8000</span>
                                          <span class="text-xs">
                                            145 advertisers + 1500 views + 6days share
                                            </span>
                                        </div>
                                    </label>

                                </li>
                            </ul>

                            <button class="text-white font-mono w-full px-4 py-2 bg-green-900 rounded-md" type="submit">Boost Product</button>
                        </form>
                    </div>
                </label>
            </label>
        </div>


    @endforeach

livewire component

 public function boosted($id)
    {
        try{
            $user = auth()->user();
            $user->withdraw($this->amounts);
         } catch (InsufficientFunds $exception) {
             return redirect()->route('home')->with(['info' => 'insufficient funds']);
         }

        $product = Product::findOrFail($id);
        $product->update([
            'boost' => 1,
        ]);

        return redirect()->route('home')->with(['success' => 'Advert placed succefully']);
    }
0 likes
1 reply
Snapey's avatar
Snapey
Best Answer
Level 122

You submit the form for boosted and then return to another controller. How can it update more than one product?

Please or to participate in this conversation.