Home  >  Article  >  PHP Framework  >  How to add, delete, modify and check users in laravel

How to add, delete, modify and check users in laravel

PHPz
PHPzOriginal
2023-04-13 10:46:19749browse

Laravel is an open source framework based on PHP. It provides many convenient tools and components so that developers can quickly build web applications. This article will introduce how to add, delete, modify and query users in Laravel.

1. Add users

In Laravel, we can use the Artisan command line tool to quickly generate model, migration, controller and other files. First enter the following command on the command line to create a User model:

php artisan make:model User

Then, we can use the following command to generate a migration of users:

php artisan make:migration create_users_table --create=users

In the migration file, we can use The following code creates a users data table:

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

Next, we can add the following method in the controller to handle the request to add a user:

public function store(Request $request)
{
    $validatedData = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:8',
    ]);

    User::create([
        'name' => $validatedData['name'],
        'email' => $validatedData['email'],
        'password' => Hash::make($validatedData['password']),
    ]);

    return redirect('/users')->with('success', 'User created successfully!');
}

In this method, we first process the request The data is validated and then the user data is saved to the database using the User model's create method. Finally, we redirect to the user list page and return a success message.

2. Find users

To find users, we can use Laravel’s query builder. The following is a simple example of querying all users:

$users = DB::table('users')->get();

We can also use the where method to filter users who meet the conditions, for example:

$users = DB::table('users')
                ->where('name', 'like', '%John%')
                ->get();

The above example will find all names containing "John " User.

3. Update users

Updating users is similar to adding users. We need to add a method to handle update requests in the controller:

public function update(Request $request, $id)
{
    $validatedData = $request->validate([
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users,email,'.$id,
        'password' => 'nullable|string|min:8',
    ]);

    $user = User::find($id);

    $user->name = $validatedData['name'];
    $user->email = $validatedData['email'];

    if (!empty($validatedData['password'])) {
        $user->password = Hash::make($validatedData['password']);
    }

    $user->save();

    return redirect('/users')->with('success', 'User updated successfully!');
}

In this method, we first verify the requested data, then find the corresponding user and update the user data. Finally, we redirect to the user list page and return a success message.

4. Delete users

To delete a user, we can use the following code:

public function destroy($id)
{
    $user = User::findOrFail($id);
    $user->delete();

    return redirect('/users')->with('success', 'User deleted successfully!');
}

In this method, we first find the corresponding user based on the user ID and call delete method deletes it from the database. Finally, we redirect to the user list page and return a success message.

Conclusion

The above are the basic operations for adding, deleting, modifying and checking users in Laravel. This is just a simple example, there may be more complex situations that need to be dealt with in actual development. However, Laravel provides many tools and components that can greatly simplify the development process, allowing us to build high-quality web applications faster.

The above is the detailed content of How to add, delete, modify and check users in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn