php - 如何创建功能搜索框来查询某个表中的数据?

标签 php mysql laravel laravel-4

我想创建功能性搜索框来查询某个表中的数据。

数据库详细信息:

  • 表名称:库存
  • 主键:id
  • 唯一索引:sku
  • 大小 = 120, 000 行
<小时/>

搜索框详细信息:

Blade /HTML

            {{ Form::open(array('action' => 'UrlController@query', 'method' => 'get')) }}

                <input autofocus name="search" id="search" placeholder="Enter Catalog # " type="text" />

                <span class="pull-right">

                    {{ Form::submit('Search', array('class' => 'btn btn-success btn-sm')) }}

                </span>

            {{ Form::close() }}

enter image description here

如您所见,我的搜索框名称是search

<小时/>

Controller 功能:

public function query() {

        // grab GET parameters
        $search = Input::get('search');

        // apply search filter
        if ($search){

            //$inventories = Inventory::all(); # It's works if I do this

            $inventories = Inventory::where(function($inventories) use ($search){
                $inventories->where('search', 'like', '%'.$search.'%');
            });


            return View::make('inventories.index')
            ->with('inventories', $inventories);
        }else

        return View::make('inventories.index');

    }
<小时/>

最后:

我无法显示任何结果。

我做错了什么吗?这里:

$inventories = Inventory::where(function($inventories) use ($search){
                $inventories->where('search', 'like', '%'.$search.'%');
            });

在 Laravel 中完成此类任务最有效的方法是什么?

最佳答案

您没有使用get()方法

$inventories = Inventory::where(function($inventories) use ($search){
                $inventories->where('search', 'like', '%'.$search.'%');
            })->get();

并且还使用paginate()方法

$inventories = Inventory::where(function($inventories) use ($search){
                    $inventories->where('search', 'like', '%'.$search.'%');
                })->paginate(15);

希望对大家有帮助

关于php - 如何创建功能搜索框来查询某个表中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28862297/

相关文章:

Java - 另一个 "BitmapFactory.decodeByteArray always null"

php - CakePHP : How to Create OptGroup Drop down list from DataBase?

php - 在 SQL 字段中有一个数组。如何系统地展示它?

mysql - 如何从保存日期大于另一个日期 + 1 周的数据库中获取数据

Laravel 5 php artisan 未在 Vagrant 中运行,缺少供应商/services.json?

php - Mysql连接3个具有多个字段的表

php - Yii2查询错误

mysql - SELECT 查询从两个表中收集结果

PHP 5.3.2 在匿名函数中使用 $this 的替代方案?

javascript - 有没有办法使用 AJAX 将响应结果传递给另一个页面