Php 拉威尔:我无法填写搜索表
我正在为Php 拉威尔:我无法填写搜索表,php,sql,laravel,forms,search,Php,Sql,Laravel,Forms,Search,我正在为帖子表构建搜索表单 现在我可以搜索描述,但当我选择一个类别或日期并进行搜索时,结果页面会显示所有帖子。这意味着“类别”和“日期”的搜索功能不起作用 我不想构建一个严格的搜索功能,所以我使用了orWherequery子句 web.php Route::get('results','ResultsController@index')->name('posts.result'); resultcontroller.php 公共功能索引(请求$Request) { $keyword=$requ
帖子
表构建搜索表单
现在我可以搜索描述,但当我选择一个类别或日期并进行搜索时,结果页面会显示所有帖子。这意味着“类别”和“日期”的搜索功能不起作用
我不想构建一个严格的搜索功能,所以我使用了orWhere
query子句
web.php
Route::get('results','ResultsController@index')->name('posts.result');
resultcontroller.php
公共功能索引(请求$Request)
{
$keyword=$request->get('keyword');
$category=$request->get('category_id');
$date=$request->get('date');
如果($keyword | |$category | |$date){
$posts=Post::Where('keyword'、'LIKE'、'%'。$keyword.'%'))
->orWhere('category_id',$category)
->或其中('date',$date)
->分页(10);
返回视图('posts.result',压缩('posts');
}否则{
$posts=Post::latest()
->订购人(“日期”、“asc”)
->分页(10);
返回视图('posts.result',压缩('posts');
}
}
发布表迁移
Schema::create('posts',函数(Blueprint$table){
$table->bigIncrements('id');
$table->string('image');
$table->unsignedbiginger('category_id');
$table->string('title');
$table->string('place');
$table->string('map');
$table->date('date');
$table->string('organizer');
$table->string('organizer_link');
$table->timestamp('published_at')->nullable();
$table->text('description');
$table->timestamps();
});
result.blade.php
类别
@foreach(App\Category::all()作为$Category)
{{$category->name}
@endforeach
我试图调整您的代码,使其只包含实际传递的搜索参数。但是,无法测试它,因为我必须离开,所以请测试它是否有效。代码应该与独立传递的任何代码一起工作,这就是为什么我必须在其中执行搜索
数组和匹配
键的原因
public function index(Request $request)
{
$keyword = $request->get('keyword');
$category_id = $request->get('category_id');
$date = $request->get('date');
if ($keyword || $category_id || $date) {
$search = [];
if (!empty($keyword)) {
$search[] = [
'column' => 'keyword',
'value' => "%{$keyword}%",
'match' => 'LIKE',
];
}
if (!empty($category_id)) {
$search[] = [
'column' => 'category_id',
'value' => $category_id,
'match' => '=',
];
}
if (!empty($date)) {
$search[] = [
'column' => 'date',
'value' => $date,
'match' => '=',
];
}
// by here, we have for sure 1 condition
$posts = Post::where($search[0]['column'], $search[0]['match'], $search[0]['value']);
if (count($search) > 1) {
// multiple conditions, using orWhere
array_shift($search);
foreach ($search as $condition) {
$posts->orWhere($condition['column'], $condition['match'], $condition['value']);
}
}
$posts = $posts->paginate(10);
} else {
$posts = Post::latest()
->orderBy('date', 'asc')
->paginate(10);
}
return view('posts.result', compact('posts'));
}
您的表格:
<form action="{{ route('posts.result') }}" class="search" method="GET">
<input type="text" name="keyword" class="search_text" placeholder="Enter the key words">
<select name="category_id" id="select" class="Genre">
<option value="" hidden>Category</option>
@foreach(App\Category::all() as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
<input type="text" name="date" class="search_date" id="date" placeholder="Choose date">
<button type="submit" class="test">
<i class="fas fa-search "></i>
</button>
</div>
</form>
类别
@foreach(App\Category::all()作为$Category)
{{$category->name}
@endforeach
我试图调整您的代码,使其只包含实际传递的搜索参数。但是,无法测试它,因为我必须离开,所以请测试它是否有效。代码应该与独立传递的任何代码一起工作,这就是为什么我必须在其中执行搜索
数组和匹配
键的原因
public function index(Request $request)
{
$keyword = $request->get('keyword');
$category_id = $request->get('category_id');
$date = $request->get('date');
if ($keyword || $category_id || $date) {
$search = [];
if (!empty($keyword)) {
$search[] = [
'column' => 'keyword',
'value' => "%{$keyword}%",
'match' => 'LIKE',
];
}
if (!empty($category_id)) {
$search[] = [
'column' => 'category_id',
'value' => $category_id,
'match' => '=',
];
}
if (!empty($date)) {
$search[] = [
'column' => 'date',
'value' => $date,
'match' => '=',
];
}
// by here, we have for sure 1 condition
$posts = Post::where($search[0]['column'], $search[0]['match'], $search[0]['value']);
if (count($search) > 1) {
// multiple conditions, using orWhere
array_shift($search);
foreach ($search as $condition) {
$posts->orWhere($condition['column'], $condition['match'], $condition['value']);
}
}
$posts = $posts->paginate(10);
} else {
$posts = Post::latest()
->orderBy('date', 'asc')
->paginate(10);
}
return view('posts.result', compact('posts'));
}
您的表格:
<form action="{{ route('posts.result') }}" class="search" method="GET">
<input type="text" name="keyword" class="search_text" placeholder="Enter the key words">
<select name="category_id" id="select" class="Genre">
<option value="" hidden>Category</option>
@foreach(App\Category::all() as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@endforeach
</select>
<input type="text" name="date" class="search_date" id="date" placeholder="Choose date">
<button type="submit" class="test">
<i class="fas fa-search "></i>
</button>
</div>
</form>
类别
@foreach(App\Category::all()作为$Category)
{{$category->name}
@endforeach
您是否收到任何错误?到底是什么不起作用?我还看到你在类别和日期中使用了orWhere,但没有类似的内容。但是,如果不输入其中任何一个,您的查询将不会返回任何内容。因此,如果存在,是否应该有条件地将它们添加到查询中?你假设如果其中任何一个都存在,那么所有的都存在。从你的问题中我们看不出来。我没有任何错误。我的问题是,当我选择一个类别或日期并进行搜索时,结果页面会显示所有帖子。这意味着“类别”和“日期”的搜索功能不起作用。您可以共享dd($category,$date);值?请将这些值作为条件添加到查询中。请确保仅当它们确实传递了数据时才将它们添加到查询中。@Mesuti两个值都为null。是否有任何错误?到底是什么不起作用?我还看到你在类别和日期中使用了orWhere,但没有类似的内容。但是,如果不输入其中任何一个,您的查询将不会返回任何内容。因此,如果存在,是否应该有条件地将它们添加到查询中?你假设如果其中任何一个都存在,那么所有的都存在。从你的问题中我们看不出来。我没有任何错误。我的问题是,当我选择一个类别或日期并进行搜索时,结果页面会显示所有帖子。这意味着“类别”和“日期”的搜索功能不起作用。您可以共享dd($category,$date);值?请将这些值作为条件添加到查询中。请确保仅当它们确实传递了数据时才将它们添加到查询中。@Mesuti这两个值都为空。但我按说明更改了关键字。因为我的表中没有关键字列。不客气。是的,您可以随意更改,代码易于阅读和更改:)。做你必须做的,使它工作,但我改变了描述关键字。因为我的表中没有关键字列。不客气。是的,您可以随意更改,代码易于阅读和更改:)。做你必须做的事情让它工作