首页 > 解决方案 > 如何添加到我的搜索链接?search="word"?

问题描述

我需要从类似的搜索页面添加到我的路线topic/category?search="word",但我不知道如何添加它。

现在,在我看来,我在内容上方得到了这个词:

 @if(isset($details))
        <p> The Search results for your query <b> {{ $query }} </b> are :</p>
@endif
<form action="/topic/{{strtolower($category->category)}}/career-solutions" method="POST" role="search">
     {{ csrf_field() }}
     <input type="hidden" name="c" value="{{$category->id }}">
     <input type="hidden" name="d" value="{{$category->category }}">
     <div class="input-group">
         <input type="text" class="form-control" name="q"
             placeholder="Search content"> <span class="input-group-btn">
             <button type="submit" class="btn btn-default">
                 <span class="glyphicon glyphicon-search"></span>
             </button>
         </span>
     </div>
 </form>

这是我的路线和我尝试的方法:

Route::any ( '/topic/{category?}/career-solutions/{q?}/{page?}', 'CategoryController@searchCareer' );

这是我的控制器:

$q = Input::get ( 'q' );
$user = CareerSolution::where ( 'subject', 'LIKE', '%' . $q . '%' )
                      ->where('career_solutions.topic_category_id', '=', $c)
        ->join('role_users' , 'role_users.user_id', '=', 'career_solutions.user_id')
        ->join('roles' , 'roles.id', '=', 'role_users.role_id')
        ->join('users', 'users.id', '=', 'career_solutions.user_id')
        ->join('categories', 'categories.id', '=', 'career_solutions.topic_category_id')
        ->orWhere ( 'career_solutions.user_id', 'LIKE', '%' . $q . '%' )
        ->orWhere ( 'career_solutions.id', '=', 'events.subject')
        ->orWhere('career_solutions.topic_category_id' ,'=', $category->id)
        ->orWhere ( 'career_solutions.user_id', '=', 'users.username')
       ->select('career_solutions.id as id','subject','users.id as user_id','username', 'profile_picture', 'role_id', 'optional', 'topic_category_id','categories.category')
        ->get();





        if (count ( $user ) > 0)
                return view ( 'category-search',$data )->withDetails ( $user )->withQuery ( $q )->with(compact('topic_id'))->with(compact('account'))->with(compact('category'))->with(compact('c'))->with(compact('d'));
        else
                return view ( 'category-search',$data )->withMessage ( 'No Details found. Try to search again !' );


现在,当我按搜索时,它会打开一个带有链接的页面/topic/courses/career-solutions,我需要添加到它?sesrch="word"...。

标签: phplaravel

解决方案


如果您使用查询参数,http 动词应该是 GET,

接下来的网址将类似于

/?x=y&p=q

所以表单动作将是

<form action="/topic/{{strtolower($category->category)}}/career-solutions?x=y&p=q" method="GET" role="search">

在你的控制器中

$x = $request->input('x');

$p = $request->input('q');

推荐阅读