如何使我的搜索框在Laravel中正常工作

问题描述 投票:0回答:2

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

    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个。”]

我本来要在我的项目中搜索动物的详细信息,但不幸的是,在我的控制器中,我不知道该如何配置。公共功能index(){$ animals = Animal :: all(); ...

php laravel eloquent laravel-blade
2个回答
0
投票

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


0
投票

您可以按以下方式修改代码

© www.soinside.com 2019 - 2024. All rights reserved.