Laravel : Search with posted parameters

I am new to Laravel so please be patient with me.

i have to implement search functionality in Laravel. i am able to send form data to the search controller and receiving it as well

public function search_results(Request $request){
        if ($request->has('gender')) {
           echo $request->gender;
        }
        .....
}

Now when i am following tutorials on the web i am getting such examples :

public function filter(Request $request, User $user)
{
    // Search for a user based on their name.
    if ($request->has('name')) {
       $user->where('name', $request->input('name'))->get();
    }

}

or

public function index()
{
    $users = User::where('status', 1)
                ->where('is_banned', 0)
                ->get(['name']);
    
    dd($users);
}

now i am not sure from where this User:: or User $user is being added, and what should i do in my scenario.

Your suggestions would be helpful ..

Thanks

Answer

The $user represents an object belonging to the model(User::) associated with a table(users) in the mvc structure. If you want to “Search for a user based on their name.” That means you already have a users table in database , and a User model .

You can use eloquent queries to retrieve data from database.

A User.php file should be generated when you create a new laravel project with composer. You can check for that.

So when you declare a variable like this :

$user = User::first();

You are actually getting the first element in ‘users’ table by using your User model.

If you can configure your User model and users table correctly , you should be able to get all records from your users table like this :

$users = User::all();

Then you can filter it like :

$users = $users->where('gender','male')      // where('gender',$request->gender) in your situation

If you dont have a users table or a model , you can look to the document about how to create models using artisan commands Laravel API Doc. Generating Model Classes