搜索 Laravel 的职位

我需要在 Laravel 中制作搜索表单


这是代码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>

这是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 !');

});

我收到错误:


调用未定义的方法 Symfony\Component\Console\Input\Input::get()


我该如何修复它?


我还想知道,如何才能先显示所有职位,然后再搜索匹配的职位?


jeck猫
浏览 122回答 2
2回答

临摹微笑

在您的代码中,该类Input引用了错误的源Symfony\Component\Console\Input\Input,这有助于从 读取输入terminal/console。您必须使用Illuminate\Support\Facades\Input来获取输入HTTP Request。或者使用Illuminate\Http\RequestRoute::any('/search', function (Illuminate\Http\Request $request) {&nbsp; &nbsp; $q = $request->get('q');&nbsp; &nbsp; ...或者Route::any('/search', function () {&nbsp; &nbsp; $q = \request('q'); // also uses Illuminate\Http\Request&nbsp; &nbsp; ...

桃花长相依

您使用了错误的Input类。use&nbsp;Illuminate\Support\Facades\Input;或者你可以简单地使用request('q')
打开App,查看更多内容
随时随地看视频慕课网APP