skip to Main Content

Before you devote my question Please here me out I know this question is asked before but still.

I Have a table inside my database called:delivery_feesmy table
it has 3 columns destination,domicileand stopdesk

  • destination is the destination
  • domicile is the shipping amount for home delivery
  • stopdesk is the shipping amount for the post office

I have a query that retrieve all destinations:

$destination = DB::table('delivery_fees')->select('destination')->pluck('destination');

I display it on a select option dropdown menu on my view like this:

<select id="wilaya" wire:model="wil" class="hidden block w-full dropdown-toggle flex items-center justify-between rounded-lg border border-jacarta-100
         bg-white py-3 px-3 dark:border-jacarta-600 dark:bg-jacarta-700 dark:text-jacarta-300" onchange="closeFunction()">
        @foreach ($destination as $items)
        <option value="{{ $items }}">{{ $items }}</option>
        @endforeach
    </select>

my goal here is when user choses a destination from the option menu I want to retrieve the related value of domicile and stopdesk I tried query builder inside my component like this:

$fees = DB::table('delivery_fees')->select('stopdesk', 'domicile')->where('destination','like',$destination)->get();

it throws Back and error of:SQLSTATE[HY093]: Invalid parameter number

Side Note
when I dd($destination) I get a collection of an array for all table value

Please Help

Thanks.

2

Answers


  1. looks like you trying t fetch the shipping fees based on the Selected destination from the dropdown menuu.
    you Should try this:

    use LivewireComponent;
    class YourLivewireComponent extends Component
    {
        public $wil;
        public $fees = [];
    
        public function updatedWil()
        {
            $this->fees = DB::table('delivery_fees')
                ->select('stopdesk', 'domicile')
                ->where('destination', $this->wil)
                ->first();
        }
    
    //other methods goes there as well
    }
    
    
        <div>
            <select id="wilaya" wire:model="wil" class="hidden block w-full dropdown-toggle flex items-center justify-between rounded-lg border border-jacarta-100 bg-white py-3 px-3 dark:border-jacarta-600 dark:bg-jacarta-700 dark:text-jacarta-300" onchange="closeFunction()">
                @foreach ($destination as $items)
                    <option value="{{ $items }}">{{ $items }}</option>
                @endforeach
            </select> 
        @if (!empty($fees))
                <p>Domicile Fee: {{ $fees->domicile }}</p>
                <p>Stopdesk Fee: {{ $fees->stopdesk }}</p>
            @endif
        </div>
    
    Login or Signup to reply.
  2. The error "SQLSTATE[HY093]: Invalid parameter number" occurs because you’re using the whereLike method with the $destination variable, which is a collection of destinations, not a single destination value. The whereLike method expects a single value to compare against.

    To fix this issue, you can modify your query to use the whereIn method, which is specifically designed to compare against a collection of values. Here’s the updated query:

    $fees = DB::table('delivery_fees')->select('stopdesk', 'domicile')
        ->whereIn('destination', $destination)
        ->get();
    

    This query will select the stopdesk and domicile columns for all destinations that match the values in the $destination collection.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search