【问题标题】:How to make my search box work properly in laravel如何让我的搜索框在 laravel 中正常工作
【发布时间】:2020-02-22 22:31:59
【问题描述】:

我打算在我的项目中搜索动物详细信息,但不幸的是在我的控制器中,我不知道如何配置它。

    public function index()
    {
        $animals =Animal::all();
        $result = false;
        if (request()->has('search') && request()->get('search') != ''){
            $result = $animals->where('serial_number', 'like', "%" .request()->get('search')."%")->get();
        }
        return view('home', ['result' => $result])->with('message', 'Below is the result');
    }

我要改变什么 这是我家的风景

    @extends('layouts.app')

    @section('content')

        <form action="{{ route('home') }}">

            <div class="p-1 bg-light rounded rounded-pill shadow-sm mb-4">
                <div class="input-group">
                    <input type="search" name="search" placeholder="Here the animal serial number..." aria-describedby="button-addon1" class="form-control border-0 bg-light">
                    <div class="input-group-append">
                        <button id="button-addon1" type="submit" class="btn btn-link text-primary"><i class="fa fa-search"></i></button>
                    </div>
                </div>
            </div>
        </form>

        @if($result)
            <div class="container">
                <div class="row justify-content-center">
                    <div class="col-md-8">
                        <div class="card">
                            <div class="card-header">
                                <h3>Details for the animal</h3>

                            </div>
                            <div class="card-body">
                                <div class="col-12">
                                    <p><strong>Id: </strong>{{ $animal->id }}</p>
                                    <p><strong>Animal: </strong>{{ $animal->type->category }}</p>
                                    <p><strong>Gender: </strong>{{ $animal->gender }}</p>
                                    <p><strong>Place Of Birth: </strong>{{ $animal->user->address->city }}</p>
                                    <p><strong>Farm: </strong>{{ $animal->user->name }}</p>
                                    <p><strong>Date: </strong>{{ $animal->created_at }}</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        @endif
    @endsection

这是我到现在为止尝试做的,但仍然遇到类似的错误

“函数 Illuminate\Support\Collection::get() 的参数太少,在第 30 行的 /Users/macair13/MeatracProject/app/Http/Controllers/HomeController.php 中传递了 0,预计至少有 1 个”

【问题讨论】:

  • request()-&gt;get('search')更改为request()-&gt;query('search')
  • 问题在于 $result = $animals->where... 。我收到此错误“Symfony\Component\Debug\Exception\FatalThrowableError Too little arguments to function Illuminate\Support\Collection::get(), 0 pass in /Users/macair13/MeatracProject/app/Http/Controllers/HomeController.php在第 30 行,预计至少有 1 个”。

标签: php laravel eloquent laravel-blade


【解决方案1】:

这就是我在 laravel 中搜索的方式:

我的表格:

<form action="{{route('search')}}" method="post">
{{csrf_field()}}
 <input type="search" name="search" placeholder="Here the animal serial number..." aria-describedby="button-addon1" class="form-control border-0 bg-light">
 <button id="button-addon1" type="submit" class="btn btn-link text-primary"><i class="fa fa-search"></i></button>

</form>

我的 Web.php:

Route::post('search','PageController@Search')->name('search');

页面控制器:

public function Search(Request $request)
  {
    $serial_number= $request->input('search');
    $results =DB::table('animals')->where(function ($query) use ($serial_number) {
     $query->where('serial_number','LIKE',"%$serial_number%");
      })->latest()->get();
    return view('search',compact('results'));

  }

【讨论】:

    【解决方案2】:

    您可以如下修改您的代码

        public function index()
        {
            if (Input::has('search') && Input::get('search') != null){
                $result = Animal::query()->where('serial_number', 'like', "%" .Input::get('search')."%")->get();
            }else{
                $result = [];
            }
            return view('home', ['result' => $result])->with('message', 'Below is the result');
        }
    

    【讨论】:

    • 这个输入类在导入什么
    • 两者是一样的,可以用request()-&gt;has('search')代替Input::has('search')
    • 它仍然给我带来一个错误,比如结果没有给我页面视图上的数据
    • 它工作得很好,但我不明白为什么它现在给我带来这个错误“这个集合实例上不存在属性 [id]。(查看:/Users/macair13/MeatracProject/ resources/views/search/index.blade.php)" 就像我没有以正确的方式传递变量
    • 如果您确定刀片上的 $result 为空,请尝试 return view('home', compact('result'));
    猜你喜欢
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 2019-08-04
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    • 2020-04-26
    相关资源
    最近更新 更多