Today let's make a step-by-step tutorial on how to use a Laravel Breeze starter kit and prepare the fundamentals to separate the role-based areas: so students, teachers, and admins could have separate design layouts, menus, and routes.
Notice: if you prefer video format, I've packaged this article also as a mini-course with 43 minutes of video, watch here.
The overall goal is to separate both the code and the visual parts, so different roles wouldn't even know that the other "portals" exist.
This is our rough plan of action:
- Install Laravel & Breeze
- First role: Creating a simple Timetable page for a student
- Second role: adding teachers, with separate routes/controllers/layouts/menus
- Register/Login: redirect to the correct route by role
- Permissions: restricting by role with Middleware
- Third role: repeating the same for Admins
At the end of this tutorial, you will also have a link to the GitHub repository.
Keep in mind that this is only one of the ways to structure such projects, during each step you may come up with a different solution and that's fine - that's the flexible beauty of Laravel.
So, are you ready?
Install Laravel and Breeze
This part is easy and standard.
laravel new project cd project composer require laravel/breeze php artisan breeze:install
And that's it, we have a default Laravel Breeze with a registration page like this:
Next, in this tutorial, I will create one simple static page for every role:
- Timetable for students
- Separate Timetable for teachers
- Student list for Admins
I deliberately won't build the functionality of those pages, because that is not the point: for your cases, you will fill it in with your different logic anyway. Here, we're focusing on separating the areas, no matter what functionality is inside, so my goal is for you to understand the principles.
First Role: Timetable for Student
In the beginning, let's assume that we have only one role, so every new user is a Student.
But, at the same time, we plan that there will be roles soon, so we need to separate the student area up front.
So, for our first page of the Student's Timetable we need these parts:
- TimetableController
- Blade view (where to put it?)
- Route line
And since we already know that there will be other roles, let's immediately separate everything into subfolders and groups.
So, we generate the controller with the Student namespace right away:
php artisan make:controller Student/TimetableController
The idea here is that all Controllers for students would be placed in that subfolder, and respectively other roles will have their subfolders.
Inside that Controller, let's fill in some fake data and load the Blade view from a subfolder, as well.
app/Http/Controllers/Student/TimetableController.php:
namespace App\Http\Controllers\Student; use App\Http\Controllers\Controller; class TimetableController extends Controller { public function index() { $timetable = [ 'Monday' => [ '09:00' => 'Lesson 1', '10:00' => 'Lesson 2', '11:00' => 'Lesson 3', '12:00' => 'Lesson 4', ], 'Tuesday' => [ '09:00' => 'Lesson 1', '10:00' => 'Lesson 2', '11:00' => 'Lesson 3', '12:00' => 'Lesson 4', ], 'Wednesday' => [ '09:00' => 'Lesson 1', '10:00' => 'Lesson 2', '11:00' => 'Lesson 3', '12:00' => 'Lesson 4', ] ]; return view('student.timetable', compact('timetable')); } }
Next, let's create a Blade view...