Search code examples
phplaravellaravel-bladelaravel-11

Laravel Search by swapping origin and destination forms


I have a project to check shipping rates using Laravel 11, I already have a table with prices that have been determined from origin to destination.

enter image description here

I use a search form with these 2 fields, but how can I swap the search, for example origin 1 to destination 2 or origin 1 to destination 2, and the results are still in the same column. And if the origin and destination are different then the results should not be displayed or will be null. For example, origin 3 to destination 2.

I use the get method but the search results do not match what is in the database table. enter image description here Why does a search with input that does not match the data table still appear with a different origin and destination? The result should be null because it is not in the column?

I have tried the following code but it doesn't work as I expected. And This is some of the code I used

From Search

<form action="{{ url('rates') }}" method="GET">
      @csrf
      <div class="form-row align-items-center row">
          <div class="col-md-5">
              <label>Origin</label>
              <select class="form-control single-select-field" name="origin">
                 <option selected>Origin...</option>
                   @foreach ($cities as $city)
                   <option value="{{ $city->id }}">{{ $city->id}}</option>
                   @endforeach
              </select>
            </div>
          <div class="col-md-5">
            <label>Destination</label>
            <select class="form-control single-select-field" name="destination">
               <option selected>Destination...</option>
                  @foreach ($cities as $city)
                    <option value="{{ $city->id }}">{{ $city->id }}</option>
                  @endforeach
            </select>
          </div>
          <div class="col-md-6 my-3">
             <button type="submit" class="btn btn-warning btn-block">Check Rates</button>
          </div>
         </div>
 </form>

Rates Controller

$origin = $request['origin'];
$destination = $request['destination'];
$result = RouteModel::where(['origin' => $origin, 'destination' => $destination])->orWhere(['destination' => $origin, 'origin' => $destination])->first();
return $result;

Thank you for the help


Solution

  • It seems like your where query has an issue, similar to the one discussed in this post.

    You cannot use a where query directly with an array in this context. Instead, you should structure the query as shown below:

    $result = RouteModel::where(function (Builder $query) use ($origin, $destination) {
            $query->where('origin', $destination)
                ->where('destination', $origin);
        })
        ->orWhere(function (Builder $query) use ($origin, $destination) {
            $query->where('origin', $origin)
                ->where('destination', $destination);
        })
        ->first();
    

    This ensures the query works as intended, checking for routes in both directions (origin to destination and vice versa).