Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

emilpapelas4@gmail.com's avatar

How can i get total price of all items that added in the right side box?

I need help to get all selected items in my project, I use livewire for that.

https://imgur.com/v8nMRMM

My View

<div class="flex flex-col bg-gray-700 rounded mb-3">
  <div class="p-2">
    <div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-3 gap-6 mx-2 sm:px-0">
      <div class="flex flex-col">
        <label for="name" class="font-medium text-sm text-gray-200">Name</label>
        <input type="text" name="search" placeholder="Search Item" wire:model="searchTerm" class="block w-full px-3 py-2 mt-2 text-gray-400 placeholder-gray-400 bg-gray-800 border border-gray-700 rounded-md focus:border-gray-500 focus:outline-none focus:ring focus:ring-gray-500 focus:ring-opacity-40">
      </div>


      <div class="flex flex-col">
        <label for="status" class="font-medium text-sm text-gray-200">Filter By</label>

        <select name="option" wire:model="orderBy" class="block w-full px-3 py-2 mt-2 text-gray-400 placeholder-gray-400 bg-gray-800 border border-gray-700 rounded-md focus:border-gray-500 focus:outline-none focus:ring focus:ring-gray-500 focus:ring-opacity-40">
          <option selected="true" value="">Choose Option</option>
          <option value="price_desc">Price - High to Low</option>    
          <option value="price_asc">Price - Low to High</option>
          <option value="discount_desc">Highest Discount</option>
          <option value="discount_asc">Lowest Discount</option>
        </select>
      </div>
    </div>
  </div>
</div>
</div>

   <div class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 gap-1 mx-2 sm:px-0">
   
    @if (is_array($items) || is_object($items))
    @foreach($items as $item)

    <div class="item_ relative cursor-pointer relative @if(in_array($item->id,$selectedItems)) included @endif" wire:click="selectProduct({{ $item->id }})"> 

    
   
<div class="w-full relative p-1 bg-gray-00 rounded overflow-hidden shadow-md border border-gray-700" style="min-height: 160px">
  <div>
 
  <div class="absolute top-3 right-3 px-3 py-0.5 bg-gray-800 rounded text-gray-200">
      <span class="text-md">FT</span>
    </div>

  <div class="h-full">
    <div class="h-20 bg-gray-900 rounded flex justify-center">         
<img class="h-20" src="{{$item->image}}">
    </div>
  </div>
</div>


<h2 class="mt-2 text-center text-gray-200 text-sm font-semibold line-clamp-1 flex justify-center">
  {{$item->name}}
</h2>

<div class="flex text-center justify-center space-x-3">

<p class="mt-2 text-gray-400 text-sm font-medium flex justify-center">  
    Steam Price
    {{$item->steam_price}}
</p>

<div class="border-r border-gray-500 h-4 flex my-3"></div>

<p class="mt-2 text-gray-400 text-sm font-medium flex justify-center flex justify-center">
    Discount   
    {{$item->discount}}
</p>

</div>

<form action="/checkout" method="POST">

@csrf

<div class="flex space-x-1">

<input class="hidden" name="item_name" value="{{$item->name}}">
<input class="hidden" name="item_price" value="{{$item->price}}">
<input class="hidden" name="item_id" value="{{$item->item_id}}">

<button type="submit" class="mt-1 px-4 py-2 bg-gray-700 hover:bg-gray-900 text-white text-sm font-medium rounded w-full transition duration-300 transform active:scale-95 ease-in-out">Order</button>

</form>

<button class="mt-1 px-4 py-2 bg-gray-700 hover:bg-gray-900 text-white text-sm font-medium rounded w-full">
<i class="fas fa-coins text-sm"></i>
{{$item->price}}
</button>
</div>

</div>

<!--
<div class="flex items-center justify-center flex-col bg-gray-700 p-4 rounded-lg w-48 space-y-4">
     <img class="rounded-sm border-gray-100 shadow-sm w-28 h-24" src="https://community.cloudflare.steamstatic.com/economy/image/" alt="user image">
    <h1 class="text-gray-50 font-semibold">eew</h1>
    <button class="px-8 py-1 border-2 border-indigo-600 bg-indigo-600 rounded-full text-gray-50 font-semibold">Buy</button>
 </div>  -->

</div>


@endforeach 
@endif
</div>

</div>

<div class="bg-gray-800 shadow-md rounded px-2 pt-3 pb-3 mb-4 flex flex-col my-2 mx-2">
    
<div class="max-w-full px-40"></div>

@foreach($getItems as $item)
      
  

      <input type="hidden" name="item_id[]" value="{{ $item['id'] }}" />
    <div class="flex items-center relative bg-gray-800 border border-gray-600 rounded overflow-hidden shadow hover:shadow-md py-1 mb-2">
    <div class="relative">
    <div class="flex items-center justify-center w-20 h-auto overflow-hidden">
	  <img src="{{ $item->image }}">
		</div>
	</div>
    	<div class="ml-3">
	  <p class="font-medium text-gray-200 text-xs">{{ $item->name }}</p>
	  <p class="text-xs text-gray-400 truncate">
      Price - {{$item->price}}
   </p></div>
    <span class="bg-gray-900 rounded px-2 mr-2 ml-auto text-sm text-white cursor-pointer" wire:click="selectProduct({{ $item->id }})">Remove</span>
  </div>


  
  <div class="relative">
<div class="absolute w-32">

<button class="bottom-5 mt-1 px-4 py-2 bg-gray-700 hover:bg-gray-900 text-white text-sm font-medium rounded w-full">
<i class="fas fa-coins text-sm"></i>
</button>

</div>
</div>

     @endforeach

</div>

</div>



</div>

My Livewire Controller

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\ItemList;
use Livewire\WithPagination;
use MercurySeries\Flashy\Flashy;
use Illuminate\Http\Request;


class SelectItems extends Component
{
    public $items;
    public $orderBy = "";
    public $selectedItems = [];
    public $odd_values = [];
    public $searchTerm = "";


    public function render()
    {
        $getItems = ItemList::whereIn('id',$this->selectedItems)->get(); 
        return view('livewire.select-items', [
            'items' => $this->items,
            'getItems' => $getItems,
        ]);
    }
    public function updatedOrderBy($option)
    {
        $this->orderBy = $option;
        if($this->orderBy == "price_desc") {
            $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->orderBy("price",'desc')->get()->take(8);
        }
        else if ($this->orderBy == "price_asc") {
            $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->orderBy("price",'asc')->get()->take(8);
        }
        else $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->get()->take(8);

        if($this->orderBy == "discount_desc")
        {
            $this->items = ItemList::orderBy('discount','desc')->get()->take(8);
        }
        else if($this->orderBy == "discount_asc")
        {
            $this->items = ItemList::orderBy('discount','asc')->get()->take(8);
        }
    }
    
    public function updatedSearchTerm($term)
    {
        if($this->orderBy == "price_desc") {
            $this->items = ItemList::where(function ($query) use ($term){
            $query->where('name','LIKE','%'.$term.'%')
                    ->orWhere('item_id', '=', $term);
        })->orderBy("price",'desc')->get()->take(12);
        }
        else if ($this->orderBy == "price_asc") {
            $this->items = ItemList::where(function ($query) use ($term){
            $query->where('name','LIKE','%'.$term.'%')
                    ->orWhere('item_id', '=', $term);
        })->orderBy("price",'asc')->get()->take(12);
        }
        else $this->items = ItemList::where(function ($query) use ($term) { $query->where('name','LIKE','%'.$term.'%') ->orWhere('item_id', '=', $term); })->get()->take(12);
    }
    
    public function selectProduct($id)
    {
        
        if(in_array($id,$this->selectedItems)){
            $itemslist = $this->selectedItems;
            if (($key = array_search($id, $itemslist)) !== false) {
                    unset($itemslist[$key]);
                }
            
            $this->selectedItems = $itemslist;
        }
        else {
            
            $this->selectedItems = array_merge($this->selectedItems, [$id]);
        }
        
    }
}

0 likes
30 replies
tykus's avatar
tykus
Best Answer
Level 104

So that is the $getItems variable, right? You can either compute the value in the Component, e.g. in the render method:

public function render()
{
    $getItems = ItemList::whereIn('id',$this->selectedItems)->get(); 
    return view('livewire.select-items', [
        'items' => $this->items,
        'getItems' => $getItems,
        'total' => $getItems->sum('price'),
    ]);
}

or, you can perform the sum in the Blade template:

{{ $getItems->sum('price') }}
1 like
emilpapelas4@gmail.com's avatar

@tykus Not works Get the following errro Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: C:\xampp\htdocs\Gpt\resources\views\livewire\select-items.blade.php)

public function render()
    {
        $getItems = ItemList::whereIn('id',$this->selectedItems)->get(); 
        return view('livewire.select-items', [
            'items' => ItemList::paginate(12),
            'getItems' => $getItems,
            'total' => $getItems->sum('price'),
        ]);
    }

Blade

{{ $items->links() }}

Also use WithPagination; and use Livewire\WithPagination; is there

tykus's avatar

@emilpapelas4@gmail.com Your updated methods are unnecessary; you can use conditional querying instead:

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\ItemList;
use Livewire\WithPagination;
use MercurySeries\Flashy\Flashy;
use Illuminate\Http\Request;

class SelectItems extends Component
{
  use WithPagination;

  public $orderBy = "";
  public $selectedItems = [];
  public $odd_values = [];
  public $searchTerm = "";

  public function render()
  {
    $getItems = ItemList::whereIn('id', $this->selectedItems)->get();
    return view('livewire.select-items', [
      'items' => $this->getItems(),
      'getItems' => $getItems,
    ]);
  }

  private function getItems()
  {
    return ItemList::query()
      ->when($this->orderBy, function ($builder, $orderBy) {
        [$column, $direction] = explode('_', $orderBy);
        return $builder->orderBy($column, $direction);
      })
      ->when($this->searchTerm, function ($builder, $term) {
        $builder->where('name', 'LIKE', '%' . $term . '%')
          ->orWhere('item_id', '=', $term);
      })->paginate();
  }

  public function selectProduct($id)
  {
    if (in_array($id, $this->selectedItems)) {
      $itemslist = $this->selectedItems;
      if (($key = array_search($id, $itemslist)) !== false) {
        unset($itemslist[$key]);
      }

      $this->selectedItems = $itemslist;
    } else {
      $this->selectedItems = array_merge($this->selectedItems, [$id]);
    }
  }
}

Get the following errro Method Illuminate\Database\Eloquent\Collection::links does not exist

Because you probably still have the updated methods which are not returning a LengthAwarePaginator instance

emilpapelas4@gmail.com's avatar

@Snapey

<?php
namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\ItemList;
use Livewire\WithPagination;
use MercurySeries\Flashy\Flashy;
use Illuminate\Http\Request;


class SelectItems extends Component
{
    use WithPagination;

    public $items;
    public $orderBy = "";
    public $selectedItems = [];
    public $odd_values = [];
    public $searchTerm = "";

    
    public function render()
    {
        $getItems = ItemList::whereIn('id',$this->selectedItems)->get(); 
        return view('livewire.select-items', [
            'items' => ItemList::paginate(12), //$this->items,
            'getItems' => $getItems,
            'total' => $getItems->sum('price'),
        ]);
    }
    public function updatedOrderBy($option)
    {
        $this->orderBy = $option;
        if($this->orderBy == "price_desc") {
            $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->orderBy("price",'desc')->get()->take(8);
        }
        else if ($this->orderBy == "price_asc") {
            $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->orderBy("price",'asc')->get()->take(8);
        }
        else $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->get()->take(8);

        if($this->orderBy == "discount_desc")
        {
            $this->items = ItemList::orderBy('discount','desc')->get()->take(8);
        }
        else if($this->orderBy == "discount_asc")
        {
            $this->items = ItemList::orderBy('discount','asc')->get()->take(8);
        }
    }
    
    public function updatedSearchTerm($term)
    {
        if($this->orderBy == "price_desc") {
            $this->items = ItemList::where(function ($query) use ($term){
            $query->where('name','LIKE','%'.$term.'%')
                    ->orWhere('item_id', '=', $term);
        })->orderBy("price",'desc')->get()->take(12);
        }
        else if ($this->orderBy == "price_asc") {
            $this->items = ItemList::where(function ($query) use ($term){
            $query->where('name','LIKE','%'.$term.'%')
                    ->orWhere('item_id', '=', $term);
        })->orderBy("price",'asc')->get()->take(12);
        }
        else $this->items = ItemList::where(function ($query) use ($term) { $query->where('name','LIKE','%'.$term.'%') ->orWhere('item_id', '=', $term); })->get()->take(12);
    }
    
    public function selectProduct($id)
    {
        
        if(in_array($id,$this->selectedItems)){
            $itemslist = $this->selectedItems;
            if (($key = array_search($id, $itemslist)) !== false) {
                    unset($itemslist[$key]);
                }
            
            $this->selectedItems = $itemslist;
        }
        else {
            
            $this->selectedItems = array_merge($this->selectedItems, [$id]);
        }
        
    }
}

Snapey's avatar

@emilpapelas4@gmail.com

can you not see it?

        $getItems = ItemList::whereIn('id',$this->selectedItems)->get(); 
        return view('livewire.select-items', [
            'items' => ItemList::paginate(12), //$this->items,
            'getItems' => $getItems,
            'total' => $getItems->sum('price'),
        ]);
tykus's avatar

@emilpapelas4@gmail.com you're effectively making two items variables available to the view template; the public property on the Component, and the data passed to the view. You cannot do both - see my version of your Component above.

emilpapelas4@gmail.com's avatar

@tykus you need the controller?

<?php
namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\ItemList;
use Livewire\WithPagination;
use MercurySeries\Flashy\Flashy;
use Illuminate\Http\Request;


class SelectItems extends Component
{
    use WithPagination;

    public $items;
    public $orderBy = "";
    public $selectedItems = [];
    public $odd_values = [];
    public $searchTerm = "";

    
    public function render()
    {
        $getItems = ItemList::whereIn('id',$this->selectedItems)->get(); 
        return view('livewire.select-items', [
            'items' => $this->items,
            'getItems' => $getItems,
            'total' => $getItems->sum('price'),
        ]);
    }
    public function updatedOrderBy($option)
    {
        $this->orderBy = $option;
        if($this->orderBy == "price_desc") {
            $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->orderBy("price",'desc')->get()->take(8);
        }
        else if ($this->orderBy == "price_asc") {
            $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->orderBy("price",'asc')->get()->take(8);
        }
        else $this->items = ItemList::where('name','LIKE','%'.$this->searchTerm.'%')->get()->take(8);

        if($this->orderBy == "discount_desc")
        {
            $this->items = ItemList::orderBy('discount','desc')->get()->take(8);
        }
        else if($this->orderBy == "discount_asc")
        {
            $this->items = ItemList::orderBy('discount','asc')->get()->take(8);
        }
    }
    
    public function updatedSearchTerm($term)
    {
        if($this->orderBy == "price_desc") {
            $this->items = ItemList::where(function ($query) use ($term){
            $query->where('name','LIKE','%'.$term.'%')
                    ->orWhere('item_id', '=', $term);
        })->orderBy("price",'desc')->get()->take(12);
        }
        else if ($this->orderBy == "price_asc") {
            $this->items = ItemList::where(function ($query) use ($term){
            $query->where('name','LIKE','%'.$term.'%')
                    ->orWhere('item_id', '=', $term);
        })->orderBy("price",'asc')->get()->take(12);
        }
        else $this->items = ItemList::where(function ($query) use ($term) { $query->where('name','LIKE','%'.$term.'%') ->orWhere('item_id', '=', $term); })->get()->take(12);
    }
    
    public function selectProduct($id)
    {
        
        if(in_array($id,$this->selectedItems)){
            $itemslist = $this->selectedItems;
            if (($key = array_search($id, $itemslist)) !== false) {
                    unset($itemslist[$key]);
                }
            
            $this->selectedItems = $itemslist;
        }
        else {
            
            $this->selectedItems = array_merge($this->selectedItems, [$id]);
        }
        
    }
}
emilpapelas4@gmail.com's avatar

@tykus tryed again and now works maybe have forget some code :)

but still need the pagination like pagination on bottom

Please or to participate in this conversation.