0

I need to make search form in Laravel

This is code welcome.blade.php:

<form action="/search" method="POST" role="search">
    {{ csrf_field() }}
    <div class="input-group">
        <input type="text" class="form-control" name="q"
               placeholder="Search job"> <span class="input-group-btn">
            <button type="submit" class="btn btn-default">
                <span class="glyphicon glyphicon-search"></span>
            </button>
        </span>
    </div>
</form>
<div class="container">
    @if(isset($details))
        <div class="row">
            @foreach( $details as $job)
                <h5>{{$job->company_name}}</h5>
                <h3>{{$job->job_name}}</h3>
            @endforeach
        </div>
    @endif
</div>

This is route in web.php:

   Route::any('/search', function () {
    $q = Input::get('q');
    $job = Job::where('job_name', 'LIKE', '%' . $q . '%')
        ->orWhere('company_name', 'LIKE', '%' . $q . '%')
        ->get();
    if (count($job) > 0)
        return view('welcome')
            ->withDetails($job)
            ->withQuery($q);
    else
        return view('welcome')
            ->withMessage('No Details found. Try to search again !');
});

I am getting an error:

Call to undefined method Symfony\Component\Console\Input\Input::get()

How I can fix it?

Also I would like to know, how could I first display all jobs, and after search only jobs that match?

tttttpr
  • 33
  • 4
  • 1
    Does this answer your question? [laravel 5 : Class 'input' not found](https://stackoverflow.com/questions/31696679/laravel-5-class-input-not-found) – Hamelraj Aug 28 '20 at 10:58

2 Answers2

3

In your code the Class Input refers wrong source Symfony\Component\Console\Input\Input which helps to read input from terminal/console.

You have to use Illuminate\Support\Facades\Input to get input from HTTP Request.

OR

Use Illuminate\Http\Request

Route::any('/search', function (Illuminate\Http\Request $request) {
    $q = $request->get('q');
    ...

OR

Route::any('/search', function () {
    $q = \request('q'); // also uses Illuminate\Http\Request
    ...
BadPiggie
  • 5,471
  • 1
  • 14
  • 28
  • Thank you! Do you know how I can display all jobs at first, and then after search display only jobs that match? Because at this time, before search my page is empty – tttttpr Aug 28 '20 at 11:31
1

You are using the wrong Input class.

use Illuminate\Support\Facades\Input;

Or you can simply use request('q')

Fawzan
  • 4,738
  • 8
  • 41
  • 85