Laravel搜索按钮单击按钮排序

时间:2018-01-23 11:22:38

标签: laravel laravel-5

可以通过单击按钮制作搜索过滤器吗?

这就是我尝试过的,我认为它的工作方式是这样的:

我在菜单中有一个搜索输入字段,在我搜索Word后,我看到了结果+排序按钮。

搜索路线:

Route::any ( '/search', function () {
    $q = Input::get ( 'q' );
    if($q != ""){
    $products = Product::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->paginate (200)->setPath ( '' );
    $pagination = $products->appends ( array (
                'q' => Input::get ( 'q' ) 
        ) );




    if (count ( $products ) > 0)
        return view ( 'search' )->withDetails ( $products )->withQuery ( $q );
    }






$s = Input::get ( 's' );
    if($s != ""){
    $products = Product::where ( 'name', 'LIKE', '%' . $s . '%' )->orWhere ( 'description', 'LIKE', '%' . $s . '%' )->paginate (200)->setPath ( '' );
    $pagination = $products->appends ( array (
                's' => Input::get ( 's' ) 
        ) );




    if (count ( $products ) > 0)
        return view ( 'search' )->withDetails ( $products )->withQuery ( $q );
    }


        return view ( 'search' )->withMessage ( 'We dont found any Products with your search query. Please try again !' );
} );

我现在添加了$s来尝试按钮:

$s = Input::get ( 's' );
        if($s != ""){
        $products = Product::where ( 'name', 'LIKE', '%' . $s . '%' )->orWhere ( 'description', 'LIKE', '%' . $s . '%' )->paginate (200)->setPath ( '' );
        $pagination = $products->appends ( array (
                    's' => Input::get ( 's' ) 
            ) );




        if (count ( $products ) > 0)
            return view ( 'search' )->withDetails ( $products )->withQuery ( $q );
        }

我的$s按钮:

<form action="/search" method="get">
  Choose your favorite subject:
  <button name="q" type="submit" value="{{ $query }}{{ $s }}">HTML</button>
</form>

Button的值仅为默认值value="{{ $query }}"

我还添加了{{ $s }}但首先我必须更改$s课程路线,以后我可以将产品排序为created_at

我想要一个这样的网址:/search?q=mysearchword&s=createdat

可能的?

如果是,我如何在&之后添加q=mysearchword以及我必须在我的路线中写下而不是$s = Input::get ( 's' );,因为它不是输入。

非常感谢!

2 个答案:

答案 0 :(得分:1)

你不能传递这样的两个参数。

相反,请使用隐藏的输入以及要传递的值。 此外,您还应该为$query var。

执行此操作
<form action="/search" method="get">
  Choose your favorite subject:
  <input type="hidden" name="s" value="{{ $s }}" />
  <input type="hidden" name="q" value="{{ $query  }}" />
  <input type="submit" value="SUBMIT" />
</form>
  

如果是,我怎么能添加&amp;在q = mysearchword之后,我必须在我的Route中写下而不是$ s = Input :: get('s');因为它不是输入。

我向您展示的方式是,您可以使用$s获取Input::get('s') var,因为它现在是输入

另一种方法,并按照你的html的方式,你可以在你的$query$s var之间添加一个分隔符,并在php中爆炸结果:

<form action="/search" method="get">
  Choose your favorite subject:
  <button name="q" type="submit" value="{{ $query }}&{{ $s }}">HTML</button>
</form>

和php:

$q = Input::get ( 'q' );
$data = explode('&', $q);
$query = $data[0];
$s = $data[1];

https://secure.php.net/manual/en/function.explode.php

答案 1 :(得分:0)

首先,使用控制器来避免路线中的意大利面条代码:

  1. 例如,修改您的路线Route::any ( '/search', ['as'=> ‘find’, 'uses' => 'SearchController@find’]);

  2. 创建SearchController.php,并在那里添加find方法

        public function find(Request $request){
    
            $input = $request->all();
            $q = $input[‘q’];
            $s = $input[‘s’];
            /*continue here your code*/
            }
    
  3. 然后将您的HTML更改为从表单发送qs

  4.     <form action="/search" method="get">
          Choose your favorite subject:
        	<input id=“q” name=“q” type="hidden" value="{{ $query}}">
        	<input id=“s” name=“s” type="hidden" value="{{ $s }}">
          <button name=“button” type="submit">HTML</button>
        </form>

相关问题