1

This is my Laravel route where I am getting value from as 'q' from a form to perform search from quiz table.

Route::get('/admin/Searchquizzes',function ($q) {      

$quizzes = DB::table('quiz')
            ->leftjoin('category', 'quiz.category_id', '=', 'category.id')
             ->where('name','LIKE','$q')
            ->select('quiz.*', 'category.name As category_name')

            ->get();
$categories = DB::table('category')->select('id', 'name')->get();

return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);
)->middleware('auth')->name('admin.Searchquizzes');

This is code for my laravel Form to perform search ..

    <div class="input-group">
      <input type="text" name="q" id="q" class="form-control" placeholder="Search...">
          <span class="input-group-btn">
            <button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
            </button>
          </span>
    </div>
  </form>

This is not working I don't know why, help shall be appreciated.

1
  • 1
    Firstly, you're missing the closing } for the closure (function). How is it not working exactly? Commented Aug 29, 2017 at 8:59

2 Answers 2

1

Fixed it by my self :)

 Route::get('/admin/Searchquizzes',function (Request $request) {
//dd($request->q);

$quizzes = DB::table('quiz')
            ->leftjoin('category', 'quiz.category_id', '=', 'category.id')
            ->where('quiz.name','LIKE',$request->q)
            ->select('quiz.*', 'category.name As category_name')
            ->get();

$categories = DB::table('category')->select('id', 'name')->get();

return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);

})->middleware('auth')->name('admin.Searchquizzes');

Here is my form , Now i can search quizes :)

<form method="get" action="{{route('admin.Searchquizzes')}}">
{{ csrf_field() }}
<div class="input-group">
    <input type="text" name="q" id="q" class="form-control" placeholder="Search...">
    <span class="input-group-btn">
    <button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
    </button>
    </span>
</div>
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

YOu won't get that form value. You are not using POST method

<form method="POST" action="{{route('admin.Searchquizzes')">
    {{ csrf_field() }}
    <div class="input-group">
        <input type="text" name="q" id="q" class="form-control" placeholder="Search...">
        <span class="input-group-btn">
        <button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
        </button>
        </span>
    </div>
</form>

Route :

use Illuminate\Http\Request;

Route::POST('/admin/Searchquizzes',function (Request $request) {
    //dd($request->q);

    $quizzes = DB::table('quiz')
                ->leftjoin('category', 'quiz.category_id', '=', 'category.id')
                ->where('name','LIKE','$q')
                ->select('quiz.*', 'category.name As category_name')
                ->get();

    $categories = DB::table('category')->select('id', 'name')->get();

    return view('admin/quizlisting', ['quizzes' => $quizzes, 'categories' => $categories]);

})->middleware('auth')->name('admin.Searchquizzes');

1 Comment

sir i need to search specific 'Quiz' from my search bar from an existing table of quizzes so i should use 'Route::get' instead of post plzz clear me if i'm wrong .. Your solutins giving erors "Integrity constraint violation: 1052 Column 'name' in where clause is ambiguous (SQL: select quiz.*, category.name as category_name from quiz left join category on quiz.category_id = category.id where name LIKE $q)" this is eror statement

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.