Setting up multi-authentication in Laravel 11 using Breeze, along with CRUD functionalities for an admin panel, involves several steps. Below is a guide to help you achieve this:
Step 1: Install Laravel
First, create a new Laravel project if you haven’t done so yet:
#bash
composer create-project laravel/laravel laravel-multi-auth
cd laravel-multi-auth
Step 2: Install Laravel Breeze
Install Laravel Breeze, which provides a simple authentication setup:
#bash
composer require laravel/breeze --dev
php artisan breeze:install
npm install && npm run dev
php artisan migrate
Step 3: Create Admin and User Models
Next, you’ll need to create a new Admin
model alongside the default User
model. Use the following command to generate the model and migration:
#bash
php artisan make:model Admin -m
Edit the migration file located at database/migrations/xxxx_xx_xx_create_admins_table.php
to define the fields for your admins
table:
#php
public function up()
{
Schema::create('admins', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
});
}
Run the migration to create the table:
#bash
php artisan migrate
Step 4: Set Up Authentication for Admins
You need to set up authentication for admins. In your Admin
model, make sure to implement the Authenticatable
interface:
#php
namespace App\Models;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable implements AuthenticatableContract
{
protected $guarded = [];
protected $hidden = ['password', 'remember_token'];
}
Step 5: Create Authentication Controllers
Generate authentication controllers for the admin:
#bash
php artisan make:controller AdminAuthController
In AdminAuthController
, add methods for registration, login, and logout. Here’s a simple example for registration:
#php
namespace App\Http\Controllers;
use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class AdminAuthController extends Controller
{
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:admins',
'password' => 'required|string|min:8|confirmed',
]);
if ($validator->fails()) {
return response()->json($validator->errors(), 422);
}
$admin = Admin::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);
return response()->json($admin, 201);
}
public function login(Request $request)
{
// Similar to registration, validate and log in
}
public function logout(Request $request)
{
// Handle logout
}
}
Step 6: Define Routes
Define routes for admin authentication in routes/web.php
:
#php #laravel
use App\Http\Controllers\AdminAuthController;
Route::prefix('admin')->group(function () {
Route::post('register', [AdminAuthController::class, 'register']);
Route::post('login', [AdminAuthController::class, 'login']);
Route::post('logout', [AdminAuthController::class, 'logout']);
});
Step 7: Protect Routes with Middleware
You can use middleware to protect routes, ensuring only authenticated users can access specific routes. Create a middleware if needed:
#bash
php artisan make:middleware AdminMiddleware
In the middleware, check if the user is an admin. Then apply the middleware to your routes.
Step 8: Create CRUD Functionality
You can create a resource controller for managing CRUD operations. For example, to manage users:
#bash
php artisan make:controller UserController --resource
Implement the necessary CRUD methods (index, create, store, show, edit, update, destroy) within the UserController
.
Step 9: Set Up Views
If you’re using Blade templates, create views for your admin panel. Use @can
directives to check permissions for different actions.
Step 10: Testing
Ensure you thoroughly test your authentication and CRUD operations. You can use tools like Postman for API testing.
This setup gives you a basic multi-authentication system in Laravel 11 with an admin panel capable of CRUD operations. Customize the views and logic according to your application requirements. If you need further enhancements, consider using policies and gates for finer access control.
Streamlining Your Development Process with Coding Filters!
Incorporating coding filters into your development workflow helps streamline the process by eliminating redundancy and unnecessary operations. Filters allow developers to focus only on the essential elements of their code, improving performance, reducing errors, and making the development process more efficient.