【问题标题】:Laravel naming 'Where' and AJAX statementsLaravel 命名 'Where' 和 AJAX 语句
【发布时间】:2018-01-15 02:34:12
【问题描述】:

这是我找不到文档的东西。我需要对我的页面进行多次 AJAX 搜索。它们都来自同一个数据库。搜索的唯一区别是它们可能只需要获取和搜索一列。

例如:我的页面包含一个搜索整个数据库的输入、一个允许我按国家/地区过滤的选择以及另一个允许我按类型过滤的过滤器。

我遇到的问题是我无法,例如让选择只搜索country 列。它搜索整个数据库,而我只需要它搜索一列。

问题在于我对所有事情都使用一个控制器 - 当您只需要搜索整个数据库时它就可以工作。仅搜索指定列时,它不起作用。这是有问题的控制器:

$launchsitesatellite = DB::table('satellites')
    ->where(function($q) use ($request) {
        $q->orWhere('satname','LIKE','%'.$request->search.'%')
            ->orWhere('norad_cat_id','LIKE','%'.$request->search.'%')
            ->orWhere('country','LIKE','%'.$request->search.'%')
            ->orWhere('object_id','LIKE','%'.$request->search.'%');
        })
->where('site', $site_code)->get();

此控制器允许轻松搜索我的数据库。我现在将显示我的 AJAX 请求:

$("#filter-country").change(function() {
    $value=$(this).val();
    $.ajax({
        type: "get",
        url: "{{$launchsitename->site_code}}",
        data: {'search':$value, type:'country'},
        success: function(data){
          $('#launchsatdisplay').html(data);
        }
    });
});

这会获取用户给出的搜索请求。这再次完美运行并设法获得我想要的结果。唯一的问题是它搜索我的整个数据库,而我只想搜索我数据库中的country 列。

基本上,我的问题是:我能否命名 AJAX 请求,例如 country 并要求我的控制器只使用 ->orWhere('country') 搜索请求,所以它只使用我数据库中的 country 列?是否可以以某种方式命名它们?

【问题讨论】:

  • 你的意思是->orWhere($request->type, 'LIKE','%'.$request->search.'%'):
  • @OmisakinOluwatobi - 不幸的是,这不起作用。每当我搜索时,我都会收到 500 服务器错误。我猜这是因为它在搜索 URL 中添加了 &type=country
  • 这意味着你必须确保'type'中的文本等同于你的数据库表中的一列
  • @OmisakinOluwatobi 我可以确认文本相当于一列。
  • @OmisakinOluwatobi - 我设法修复它!唯一的问题是它仍然搜索整个数据库而不是列。我们需要一些只要求 AJAX 使用 where 语句和 type 的东西。

标签: php jquery mysql ajax laravel


【解决方案1】:

由于您使用type 指定搜索类型,您可以在子查询中使用它:

$launchsitesatellite = DB::table('satellites')
    ->where(function($q) use ($request) {
        if(empty($request->type) && empty($request->rocket_type)) {
            $q->orWhere('satname','LIKE','%'.$request->search.'%')
                ->orWhere('norad_cat_id','LIKE','%'.$request->search.'%')
                ->orWhere('country','LIKE','%'.$request->search.'%')
                ->orWhere('object_id','LIKE','%'.$request->search.'%');
        } else {
            if(!empty($request->type)) { // If the type is specified, use it
                $q->orWhere($request->type,'LIKE','%'.$request->search.'%');

            }
            if(!empty($request->rocket_type)) { // If the rocket_type is specified, use it
                $q->orWhere('rocket_type','LIKE','%'.$request->rocket_type.'%');

            }
        }

    })
    ->where('site', $site_code)->get();

【讨论】:

  • 谢谢! - 完美运行!一件事-如果我想添加另一个搜索,例如只过滤列rocket_type,那么流程是什么。我会添加另一个 orWhere 查询还是做其他事情?
  • 您只需将其添加到 else 即可进行整个数据库搜索。 if 块将处理您作为类型传入的任何内容。
  • 我刚刚尝试将->orWhere($request->rocket_type,'LIKE','%'.$request->search.'%'); 添加到else 语句中,但这并没有产生任何结果。我用type 替换了rocket_type。你能确认我这样做是否正确吗?
  • 不,如果您将 rocket_type 作为搜索类型/列传递,请不要使用 request->type。那里不需要更改。您只需将->orWhere('rocket_type','LIKE','%'.$request->search.'%') 添加到else 块中即可。
  • 不幸的是,这不起作用。不过,它仍然适用于country,就像我原来的问题一样。
猜你喜欢
  • 2017-06-10
  • 2014-10-16
  • 1970-01-01
  • 2016-02-02
  • 1970-01-01
  • 2018-07-11
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多