【问题标题】:laravel: I cannot complete search formlaravel:我无法完成搜索表单
【发布时间】:2020-02-10 10:58:03
【问题描述】:

我正在为posts 表构建一个搜索表单。

现在我可以搜索描述,但是当我选择一个类别或日期并搜索时,结果页面会显示所有帖子。这意味着“类别”和“日期”的搜索功能不起作用。

我不想构建严格的搜索功能,所以我使用了orWhere 查询子句。

web.php

Route::get('results', 'ResultsController@index')->name('posts.result');

ResultsController.php

public function index(Request $request)
{
    $keyword = $request->get('keyword');
    $category = $request->get('category_id');
    $date = $request->get('date');

    if ($keyword || $category || $date) {
        $posts = Post::Where('keyword', 'LIKE', '%' . $keyword . '%')
            ->orWhere('category_id', $category)
            ->orWhere('date', $date)
            ->paginate(10);
        return view('posts.result', compact('posts'));
    } else {
        $posts = Post::latest()
            ->orderBy('date', 'asc')
            ->paginate(10);
        return view('posts.result', compact('posts'));
    }
}

发布表迁移

Schema::create('posts', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('image');
    $table->unsignedBigInteger('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

<form action="{{ route('posts.result') }}" class="search" method="GET">
    <input type="text" name="description" class="search_text" placeholder="Enter the key words">
    <select name="category" id="select" class="Genre">
        <option value="" hidden name="category">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>

【问题讨论】:

  • 您有任何错误吗?究竟是什么不工作?我还看到您在类别和日期中使用 orWhere 而不喜欢。但如果不输入任何一个,您的查询将不会返回任何内容。所以如果它们存在的话,也许应该有条件地将它们添加到查询中?您假设如果其中任何一个存在,那么全部存在。我们无法从您的问题中看出这一点。
  • 我没有收到任何错误。我的问题是,当我选择一个类别或日期并进行搜索时,结果页面会显示所有帖子。这意味着“类别”和“日期”的搜索功能不起作用。
  • 你能分享一下dd($category, $date);价值观?
  • 请在您的查询中添加这些作为条件。确保仅在它们确实传递了数据时才将它们添加到查询中。
  • @Mesuti 两个值都为空。

标签: php sql laravel forms search


【解决方案1】:

我尝试将您的代码调整为仅包含实际传递的搜索参数。但是,由于我必须离开,所以无法测试它,所以请测试它是否有效。该代码应该适用于任何独立传递的代码,这就是为什么我必须在其中执行search 数组和match 键。

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>

【讨论】:

  • 但我通过描述更改了关键字。因为我的表中没有关键字列。
  • 不客气。是的,你可以改变任何你想要的,代码很容易阅读和改变:)。做你必须做的事情来让它发挥作用
猜你喜欢
  • 2011-08-29
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 2017-03-28
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多