Home  >  Article  >  PHP Framework  >  How to delete user in Laravel

How to delete user in Laravel

PHPz
PHPzOriginal
2023-04-21 10:09:04610browse

The Laravel framework provides powerful functions to operate databases. One of the common functions is to delete records in the database. In many applications, we need to delete users. This article will explain how to delete a user in Laravel.

First, we need a user model. If you already have one, then skip this step. The following is a simple user model:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $guarded = [];
}

In this model, we use Laravel’s built-in Authenticatable class. A $guarded attribute is also defined, which tells Laravel which properties cannot be batch assigned. In this case, we are not restricting any properties.

Next, we need a controller to handle the operation of deleting the user. Suppose we already have a UserController controller and define a destroy method. This method will pass a user ID in the request and delete the corresponding user. Here is a basic UserController controller:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function destroy(Request $request)
    {
        $user = User::find($request->id);
 
        if ($user) {
            $user->delete();
            return redirect('/users')->with('success', 'User deleted successfully');
        }
 
        return redirect('/users')->with('error', 'User not found');
    }
}

In this controller we first get the user ID passed in the request and then look up the user via the User model Record. If the user is found, we call the delete method to delete the user record. If the user record does not exist, we will redirect to the user list page and display an error message.

Now, we need a route to handle delete user requests. The following is a routing definition:

Route::delete('/users/{id}', 'UserController@destroy')->name('users.delete');

In this routing, we use RESTful style routing. It maps the HTTP DELETE request to the destroy method of the UserController controller, and passes the parameter {id} in the request as a parameter to the method.

Next, we need a view to display the user list and perform deletion operations. Here is a simple user list view:

@extends('layouts.app')
 
@section('content')
    <div class="container">
        @if (session('success'))
            <div class="alert alert-success">
                {{ session('success') }}
            </div>
        @endif
 
        @if (session('error'))
            <div class="alert alert-danger">
                {{ session('error') }}
            </div>
        @endif
 
        <h3>Users</h3>
 
        <table class="table">
            <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Email</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            @foreach($users as $user)
                <tr>
                    <td>{{ $user->id }}</td>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                    <td>
                        <form action="{{ route(&#39;users.delete&#39;, $user->id) }}" method="POST">
                            @csrf
                            @method('DELETE')
 
                            <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                        </form>
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    </div>
@endsection

In this view, we first check if there are any success or error messages. If there are, we display them. We then listed all users and placed a delete button at the end of each row. When the user clicks the delete button, we will submit a DELETE request in the form to the users.delete route, passing the user's ID as a parameter.

Now, we have completed the operation of deleting users in Laravel. When we click the delete button in the user list, we will execute the destroy method of the UserController controller, delete the corresponding user, and redirect the user to the user list page while displaying Success or error message.

Overall, deleting users in Laravel is very simple. We only need to build a controller, define the corresponding routes, build a view, and set up the relevant models. The Laravel framework provides many powerful tools to handle data operations, which allows us to quickly develop feature-rich applications.

The above is the detailed content of How to delete user 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