gpt4 book ai didi

php - 如何在 Controller 上使用 if else 来实现搜索功能?

转载 作者:行者123 更新时间:2023-11-29 17:56:31 25 4
gpt4 key购买 nike

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

Controller :

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);
}

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

@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

路线:

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

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

最佳答案

如果您想在 @if包含另一个snippet,那么您可以这样做:

@include('route.to.view')

它将在该 URL 中搜索 blade View ,并在存在 $data 时插入 html

关于php - 如何在 Controller 上使用 if else 来实现搜索功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48762608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com