gpt4 book ai didi

php - 如何在 laravel 中使用多个输入字段在 laravel 中实现搜索功能

转载 作者:行者123 更新时间:2023-12-03 23:04:41 26 4
gpt4 key购买 nike

我正在使用 laravel 和 vue.js 制作在线票务服务。我想在我的网站上实现多个字段的搜索功能。例如:航类目的地、到达时间、日期。我不知道如何在 laravel 中实现它。我看过几个视频,但它对我不起作用。有人可以知道我应该怎么做吗?

这是我的输出 enter image description here

索引.blade.php

         <h2 class="text-4 mb-3">Book Domestic and International Flights</h2>
<form id="bookingFlight" action="{{url("/flightSearch")}}">
{{ csrf_field() }}
<div class="form-row">
<div class="col-md-8 col-lg-3 form-group">
<input type="text" class="form-control" id="flightFrom" required placeholder="From">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span> </div>
<div class="col-md-8 col-lg-3 form-group">
<input type="text" class="form-control" id="flightTo" required placeholder="To">
<span class="icon-inside"><i class="fas fa-map-marker-alt"></i></span> </div>
<div class="col-md-8 col-lg-3 form-group">
<input id="flightDepart" type="text" class="form-control" required placeholder="Depart Date">
<span class="icon-inside"><i class="far fa-calendar-alt"></i></span> </div>
<div class="col-md-8 col-lg-3 form-group">
<input id="flightReturn" type="text" class="form-control" required placeholder="Return Date">
<span class="icon-inside"><i class="far fa-calendar-alt"></i></span> </div>

<div class="col-md-12 form-group">
<button class="btn btn-primary btn-block" type="submit">Search</button>
</div>
</div>
</form>
</div>

FlightsController.php


namespace App\Http\Controllers;

use App\Flights;
use Illuminate\Http\Request;

class FlightsController extends Controller
{
public function searchFlights(Request $request){
return $request->all();
}
}

网络.php

Route::get('/flightSearch', 'FlightsController@searchFlights')->name('flightSearch');

航类.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flights extends Model
{
protected $fillable = [
'flightFrom', 'flightTo', 'flightDepart', 'flightReturn',
];
}

最佳答案

首先您需要将方法添加到表单并将名称添加到输入

<form method="GET" id="bookingFlight" action="{{url("/flightSearch")}}">
{{ csrf_field() }}
<input type="text" name="flightFrom" class="form-control" id="flightFrom" required placeholder="From">
<input type="text" name="flightTo" class="form-control" id="flightTo" required placeholder="To">
<input name="flightDepart" id="flightDepart" type="text" class="form-control" required placeholder="Depart Date">
<input name="flightReturn" id="flightReturn" type="text" class="form-control" required placeholder="Return Date">
<button class="btn btn-primary btn-block" type="submit">Search</button>
</form>

然后就可以根据请求数据进行查询了:

public function searchFlights(Request $request){
return Flights::where('flightFrom', 'like', '%' . $request->flightFrom . '%')
->where('flightTo', 'like', '%' . $request->flightTo . '%')
->where('flightDepart', 'like', '%' . $request->flightDepart . '%')
->where('flightReturn', 'like', '%' . $request->flightReturn . '%')
->get();
}


根据@RioHilmy 的评论,请注意您可以为航类出发和航类返回使用输入类型日期:

<form method="GET" id="bookingFlight" action="{{url("/flightSearch")}}">
{{ csrf_field() }}
<input type="text" name="flightFrom" id="flightFrom" required placeholder="From">
<input type="text" name="flightTo" id="flightTo" required placeholder="To">
<input name="flightDepart" type="date" id="flightDepart" required>
<input name="flightReturn" type="date" id="flightReturn" required>
<button class="btn btn-primary btn-block" type="submit">Search</button>
</form>

然后您可以使用日期值执行查询:

public function searchFlights(Request $request){
return Flights::where('flightFrom', 'like', '%' . $request->flightFrom . '%')
->where('flightTo', 'like', '%' . $request->flightTo . '%')
->whereDate('flightDepart', $request->flightDepart)
->whereDate('flightReturn', $request->flightReturn)
->get();
}

关于php - 如何在 laravel 中使用多个输入字段在 laravel 中实现搜索功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719716/

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