如何从控制器中的路径移动功能?

时间:2019-10-21 11:08:57

标签: php laravel

我通过教程进行了函数搜索,但该函数已包含在路由中,因此需要将此函数添加到控制器中。我进行了此搜索,但是现在当我搜索时,它返回一个空白页面。 / p>

这是我的观点:

<form action="/search" method="POST" role="search">
    {{ csrf_field() }}
    <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::get('topic/{category?}/{page?}', 'CategoryController@category');
Route::any ( '/search', 'CategoryController@search');

这是我的职能

public function search()
    {
      function () {
             $q = Input::get ( 'q' );
             $user = Opinion::where ( 'subject', 'LIKE', '%' . $q . '%' )->orWhere ( 'user_id', 'LIKE', '%' . $q . '%' )->get ();
              if (count ( $user ) > 0)
                  return view ( 'category-search' )->withDetails ( $user )->withQuery ( $q );
              else
                  return view ( 'category-search' )->withMessage ( 'No Details found. Try to search again !' );
        } ;
    }

我的类别搜索视图:

@extends('layout.template')

@section('content')
<title>Search results</title>


<div class="container">
    @if(isset($details))
        <p> The Search results for your query <b> {{ $query }} </b> are :</p>
    <h2>Sample Content details</h2>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>User id</th>
            </tr>
        </thead>
        <tbody>
            @foreach($details as $user)
            <tr>
                <td>{{$user->subject}}</td>
                <td>{{$user->user_id}}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    @endif
</div>

 @stop

这是我的第一个具有以下功能的路线:

Route::any ( '/search', function () {
        $q = Input::get ( 'q' );
        $category  = Category::with('event','news','opinion')->where('category_url', '=' ,$category_data)->firstOrFail();
        $data['title'] = $category->category;
        $user = Opinion::where ( 'subject', 'LIKE', '%' . $q . '%' )->orWhere ( 'user_id', 'LIKE', '%' . $q . '%' )->get ();
        if (count ( $user ) > 0)
          return view ( 'category-search' )->withDetails ( $user )->withQuery ( $q );
        else
          return view ( 'category-search' )->withMessage ( 'No Details found. Try to search again !' );
} );

2 个答案:

答案 0 :(得分:4)

删除功能(){

function search() {
    $q = Input::get ( 'q' );

        $category  = Category::with('event','news','opinion')->where('category_url', '=' ,$category_data)->firstOrFail();

        $data['title'] = $category->category;
    $user = Opinion::where ( 'subject', 'LIKE', '%' . $q . '%' )->orWhere ( 'user_id', 'LIKE', '%' . $q . '%' )->get ();
    if (count ( $user ) > 0)
        return view ( 'category-search' )->withDetails ( $user )->withQuery ( $q );
    else
        return view ( 'category-search' )->withMessage ( 'No Details found. Try to search again !' );
} );
}

答案 1 :(得分:1)

您的搜索方法必须类似于以下内容:

public function search(){
$q = Input::get ( 'q' );
$user = Opinion::where ( 'subject', 'LIKE', '%' . $q . '%' )->orWhere ( 'user_id', 'LIKE', '%' . $q . '%' )->get ();

if (count ( $user ) > 0) {
    return view ( 'category-search' )->withDetails ( $user )->withQuery ( $q );
}else{
    return view ( 'category-search' )->withMessage ( 'No Details found. Try to search again !' );
}}