3

I'm working on an app, where I should be performing search on a table containing users. However, I cannot figure out how to correctly compose the route in order to attach the view with the controller and back. My code is the following: The view:

<form action='{{url(' users')}}' method='GET'>
<input class='form-control' type="text" name="keyword" placeholder="Search for user...">
</form>

The route:

Route::get('users?keyword={$keyword}', 'UsersController@search');

The search() in UsersController:

public function search()
{
    $keyword = Request::get('keyword');

    $users = User::where("username", "LIKE","%$keyword%")
            ->orWhere("firstname", "LIKE", "%$keyword%")
            ->orWhere("lastname", "LIKE", "%$keyword%")
            ->orWhere("email", "LIKE", "%$keyword%")
            ->orWhere("phone", "LIKE", "%$keyword%")->get();

    return view('search', compact('users'));

}

Can someone please tell what would be the correct syntax? Thanks in advance!

2 Answers 2

4

Your route should be as:

 Route::get('users', 'UsersController@search');

And in your controller, you can get the query parameter by using get function of the request object, which you are doing already.

You can get the parameter by any of function as:

request()->get('keyword');
request()->input('keyword');
request()->query('keyword');

Update

Add the following import at the top of your file:

use Illuminate\Http\Request;

Then in your controller function inject it as:

public function search(Request $request)
{
    $keyword = $request->input('keyword');
    // or
    $keyword = $request->query('keyword');

    // rest of the code
}
Sign up to request clarification or add additional context in comments.

11 Comments

Then it gives the following: Missing argument 1 for get(), called in /Users/eak/Sites/laravel-exercise/app/Http/Controllers/UsersController.php on line 120 and defined.
You are using this Request::get('keyword'), right?
Yes, $keyword = Request::get('keyword'); as posted above.
Can you try this request()->query('keyword');.
Call to undefined method Illuminate\Support\Facades\Request::query()
|
2

Try this:

  Route::get('users', 'UsersController@search');

controller

public function search(Request $request) {
    $keyword = $request->get('keyword');

    $users = User::where("username", "LIKE","%$keyword%")
            ->orWhere("firstname", "LIKE", "%$keyword%")
            ->orWhere("lastname", "LIKE", "%$keyword%")
            ->orWhere("email", "LIKE", "%$keyword%")
            ->orWhere("phone", "LIKE", "%$keyword%")->get();

    return view('search', compact('users'));
}

3 Comments

I get an error: Call to undefined method Illuminate\Support\Facades\Request::get()
@sklrboy use Illuminate\Http\Request; add this after namespace.
Now my problem become, that Route::get('users', 'UsersController@search'); always redirects me to the search results, instead of the users homepage, haha. Should I maybe create another controller, or what's a professional solution in this case?

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.