用户单击“搜索”按钮后如何在视图中显示搜索结果?

时间:2018-06-29 16:07:18

标签: php laravel

我有一个带index()的UserController,它返回状态为“ D”(草稿)的会议,过去的会议和已发布的会议。

视图中有一个表单,因此用户可以引入名称来搜索用户创建的会议名称。

我的疑问是,在用户单击“搜索”按钮后,如何在视图中正确显示搜索结果。因为它本身显示为“未定义的变量:searchedConferences”。

UsersController:

class UserController extends Controller
{
    public function index(Request $request){

        $pageLimit = 5;
        $user = $request->user();

        $draftConferences = $user->conferences()->where('status','D')->paginate($pageLimit);
        $pastConferences = $user->conferences()->where('end_date','<', now())->paginate($pageLimit);
        $publishedConferences = $user->conferences()->where('status','P')->paginate($pageLimit);

        return view('users.index',
            compact('user', 'draftConferences', 'pastConferences', 'publishedConferences'));
    }
    public function searchConference(Request $request)
    {
        $user = Auth::user();
        $search = $request->search_conference;
        $conferences = Conference::where('name', 'LIKE', '%'.$search.'%')->where('creator_id',$user->id)->get();

        //dd($conferences);
       return redirect(route('user.index', ['user' => Auth::id()]) . '#myConferences')->with('searchedConferences', $conferences);
    }
}

然后在视图中,我有一个表单供用户搜索会议。而且,它的形式似乎是从index()返回的数据。有3个选项卡(“草拟会议”,“已发布的会议”,“完成的会议”),默认情况下在“会议草稿”选项卡中处于活动状态。当用户单击另一个选项卡时,它会在选项卡内容中显示该特定选项卡的结果(过去的会议,草稿会议或finsih会议)。

<form method="post" method="POST" action="{{ route('user.searchConference') }}">
    {{ csrf_field() }}
    <div class="form-group">

      <input type='text' class="form-control" placeholder="Search conferences" name="search_conference"/>
      <button>Search</button>

    </div>
</form>

@if(!empty($searchedConferences))
            {{$searchedConferences}}
        @else
            <ul class="nav nav-pills">
                <li class="nav-item">
                    <a class="nav-link active border" href="#draftConferences" data-toggle="tab" role="tab">Draft Conferences</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link border" href="#publishedConferences" data-toggle="tab" role="tab">Published Conferences</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link border" href="#finishedConferences" data-toggle="tab" role="tab">Conferences Archived</a>
                </li>
            </ul>
            <div class="tab-content" id="myTabContent">
                <div class="tab-pane fade active show clearfix" id="draftConferences" role="tabpanel" aria-labelledby="home-tab">
                    <ul class="list-group">
                    @foreach($draftConferences as $draftConference)
                        @if(!empty($draftConference))
                            <li class="list-group-item">
                                 <h5>{{$draftConference->name}}</h5>
                            </li>
                        @endif
                    @endforeach
                   <div>
                        {{$draftConferences->fragment('draftConferences')->links("pagination::bootstrap-4")}}
                    </div>
                </div>
                <!-- same structure for the "#publishedConferences" and "#finishedConferences" -->
            </div>

1 个答案:

答案 0 :(得分:0)

好像reidrect()-> with()将with()方法中的数据放入会话中。我将完全更改重定向部分,以便为新项目提供视图,或者将刀片上的变量更改为会话中的变量。大概是这样的:

{{ \Session::get('searchedConferences') }}

请参阅 https://laracasts.com/discuss/channels/general-discussion/laravel-5-redirect-with-variables?page=1是在这里找到有关此问题的有用信息的地方。