如何在控制器上搜索功能?

时间:2018-02-13 08:42:14

标签: php mysql database laravel if-statement

所以我有一个带有基本搜索功能的控制器,我想知道当用户点击搜索按钮时,如果存在数据我希望它返回带有错误消息的同一页面,但是如果数据不存在我想要返回另一页。我怎样才能做到这一点?我的代码如下:

控制器:



public function search(Request $request, $companyID, $entityType)
	{
		$data = [];
		$intentTypes = DiraQuestion::select('intent')->where('company_id', $companyID)->groupBy('intent')->get();

		foreach ($intentTypes as $intent) 
		{
			$data[$intent->intent] = $intent->intent;
		}

		$q = $request->q;
	    $user = DiraQuestion::select('eVal')->where('company_id', $companyID)->where('eType', $entityType)->where('eVal','LIKE','%'.$q.'%')->groupBy('eVal')->get();

	    return view('AltHr.Chatbot.addType', compact('data','entityType','companyID'))->withDetails($user)->withQuery($q);
	}




我当前的视图页面显示是否存在,如果不存在则显示表单:



@if(isset($details))
<div class="container-fluid container-fixed-lg">
	<div class="row">
		<div class="col-md-12">
			<div class="panel panel-default">
				<!-- <div class="panel-heading">
					<div class="panel-title"><b>{{ $query }}</b></div>
					<div class="clearfix"></div>
				</div> -->
				<div class="panel-body">
					<div class="form-group edit-response-container">
						<div class="col-md-12">
							@foreach($details as $user)
				            	<p><span style="color:red;">{{$user->eVal}}</span> is already included!</p>
				            @endforeach
				        </div>

						@if($details->isEmpty())
						<form action="{{action('AltHr\Chatbot\ChatbotController@addQType', [$companyID, $entityType])}}" method="post">
							{{csrf_field()}}
							<!-- @if(empty($data))
							<p>No predefined intents.</p>
							@else -->
							<div class="col-md-12">
								<div class="form-group form-group-default required">
									<label>New Type</label>
									<input type="text" class="form-control" name="entityValue" placeholder="{{$query}}" value="{{$query}}" required readonly/>
								</div>
							</div>
							<!-- <div class="col-md-12">
								<div class="form-group form-group-default required">
									<label>New Question</label>
									<input type="text" class="form-control" name="question" placeholder="Enter Sample Question" required>
								</div>
							</div>
							<div class="col-md-12">
								<div class="form-group form-group-default required">
									<label>Intent</label>
									<select class="full-width" name="intent" data-init-plugin="select2" required>
										<option selected disabled></option>
						                @foreach($data as $datas)
						                <option value="{{$datas}}">{{$datas}}</option>
						                @endforeach
									</select>
								</div>
							</div> -->
							<div class="col-md-12">
								<div class="new-field">
									<div class="form-group-attached">
										<div class="form-group form-group-default required">
											<label>Synonyms:</label>
											<input type="text" class="form-control" name="syn" placeholder="Enter Synonyms" />
										</div>
									</div>
								</div>
								<p><i>*Enter All Synonyms Separating With ','</i></p>
							</div>


							<div class="col-md-12">
								<br>
								<!-- <button type="button" class="btn alt-btn-green alt-btn btn-xs add-new">Add Synonym</button> -->
								<button type="submit" class="btn alt-btn-green alt-btn btn-xs">Save</button>

								<a href="{{action('AltHr\Chatbot\ChatbotController@newTest', [$companyID, $entityType])}}" class="btn alt-btn-green alt-btn btn-xs">test</a>

							</div>
							@endif
						</form>
						@endif
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
@endif
&#13;
&#13;
&#13;

路线:

&#13;
&#13;
Route::post('search/{companyID}/{entityType}','Chatbot\ChatbotController@search');
&#13;
&#13;
&#13;

正如您可以看到的,如果数据存在,它会在同一页面上显示错误消息,但如果没有数据,它会在同一页面上显示一个表单..我希望它在没有找到数据时路由到另一个页面

1 个答案:

答案 0 :(得分:0)

如果您希望在includesnippet另一个@if,那么您可以这样做:

@include('route.to.view')

将在该网址中搜索blade视图,并在存在$data的情况下插入html