draw134
draw134

Reputation: 1187

How to implement search functionality in laravel with multiple input fields in laravel

I am making a online ticketing service using laravel and vue.js. I want to implement search functionality in my website with multiple fields. eg: flight destination, arrival, date. I dont know how to implement it in laravel. I've watch several videos but it doesnt work for me. Can someone know what should i do?

Heres my output enter image description here

index.blade.php

         <h2 class="text-4 mb-3">Book Domestic and International Flights</h2>
                <form id="bookingFlight" action="{{url("/flightSearch")}}">
                        {{ csrf_field() }}
                    <div class="form-row">
                    <div class="col-md-8 col-lg-3 form-group">
                        <input type="text" class="form-control" id="flightFrom" required placeholder="From">
                        <span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span> </div>
                    <div class="col-md-8 col-lg-3 form-group">
                        <input type="text" class="form-control" id="flightTo" required placeholder="To">
                        <span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span> </div>
                    <div class="col-md-8 col-lg-3 form-group">
                        <input id="flightDepart" type="text" class="form-control" required placeholder="Depart Date">
                        <span class="icon-inside"><i class="far fa-calendar-alt"></i></span> </div>
                    <div class="col-md-8 col-lg-3 form-group">
                        <input id="flightReturn" type="text" class="form-control" required placeholder="Return Date">
                        <span class="icon-inside"><i class="far fa-calendar-alt"></i></span> </div>

                    <div class="col-md-12 form-group">
                        <button class="btn btn-primary btn-block" type="submit">Search</button>
                    </div>
                    </div>
                </form>
                </div>

FlightsController.php


namespace App\Http\Controllers;

use App\Flights;
use Illuminate\Http\Request;

class FlightsController extends Controller
{
    public function searchFlights(Request $request){
       return $request->all();
    }
}

web.php

Route::get('/flightSearch', 'FlightsController@searchFlights')->name('flightSearch');

Flights.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flights extends Model
{
    protected $fillable = [
        'flightFrom', 'flightTo', 'flightDepart', 'flightReturn',
    ];
}

Upvotes: 3

Views: 2717

Answers (1)

porloscerros Ψ
porloscerros Ψ

Reputation: 5098

first you need to add the method to the form and names to the inputs

<form method="GET" id="bookingFlight" action="{{url("/flightSearch")}}">
    {{ csrf_field() }}
    <input type="text" name="flightFrom" class="form-control" id="flightFrom" required placeholder="From">
    <input type="text" name="flightTo" class="form-control" id="flightTo" required placeholder="To">
    <input name="flightDepart" id="flightDepart" type="text" class="form-control" required placeholder="Depart Date">
    <input name="flightReturn" id="flightReturn" type="text" class="form-control" required placeholder="Return Date">
    <button class="btn btn-primary btn-block" type="submit">Search</button>
</form>

and then you can perform the query based on the request data:

public function searchFlights(Request $request){
    return Flights::where('flightFrom', 'like', '%' . $request->flightFrom . '%')
        ->where('flightTo', 'like', '%' . $request->flightTo . '%')
        ->where('flightDepart', 'like', '%' . $request->flightDepart . '%')
        ->where('flightReturn', 'like', '%' . $request->flightReturn . '%')
        ->get();
}


based on @RioHilmy comment, note you can use inputs type date for flightDepart and flightReturn:

<form method="GET" id="bookingFlight" action="{{url("/flightSearch")}}">
    {{ csrf_field() }}
    <input type="text" name="flightFrom" id="flightFrom" required placeholder="From">
    <input type="text" name="flightTo" id="flightTo" required placeholder="To">
    <input name="flightDepart" type="date" id="flightDepart" required>
    <input name="flightReturn" type="date" id="flightReturn" required>
    <button class="btn btn-primary btn-block" type="submit">Search</button>
</form>

then you can perform the query with the dates values:

public function searchFlights(Request $request){
    return Flights::where('flightFrom', 'like', '%' . $request->flightFrom . '%')
        ->where('flightTo', 'like', '%' . $request->flightTo . '%')
        ->whereDate('flightDepart', $request->flightDepart)
        ->whereDate('flightReturn', $request->flightReturn)
        ->get();
}

Upvotes: 3

Related Questions