Skip to main content

Roles/Permissions: Manage Employees

Premium
9 min read

Next on our list - separating user roles. In our system, we need admins to manage the system settings and employees, while the employees themselves can only manage customers and nothing else:

In this lesson, we will do the following:

  • Create roles Model and Database structure
  • Create a user management page (CRUD)
  • Add employees to our Customers' table and form for admins to manage
  • Add employee changes to our customer history
  • Add an additional tab in Customers for My Customers - customers assigned to the employee

Creating Roles Model and Database structure

Let's start by creating our migration file:

Migration

Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});

Then, we can fill out our Model:

app/Models/Role.php

class Role extends Model
{
protected $fillable = ['name'];
}

Of course, we should also...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (30 h 01 min)

You also get:

55 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

RZ
Roman Zelenin ✓ Link copied!

app/Models/Customer.php

if ($customer->employee_id !== $lastLog) {

Looks like this line should be:

if ($customer->employee_id !== $lastLog->employee_id) {

app/Filament/Resources/CustomerResource/Pages/ListCustomers.php

with filter query:

if (!auth()->user()->isAdmin()) {
$tabs['my'] = Tab::make('My Customers')
->badge(Customer::where('employee_id', auth()->id())->count())
->modifyQueryUsing(function ($query) {
return $query->where('employee_id', auth()->id());
});
}
M
Modestas ✓ Link copied!

Correct, updated it! Sorry about that

RZ
Rick Zimmermans ✓ Link copied!

Hi! Thank you and the team for the amazing work! Just a quick questions if i may.

in the following example, shouldn't the code be:

// From
if ($customer->employee_id !== $lastLog->employee_id) {
// To
if ($customer->employee_id !== $lastLog?->employee_id) {

Since we're only looking for records where employee_id is not null which may result in $lastLog being null.

Maybe im wrong tho. Keep up the good work!

M
Modestas ✓ Link copied!

Hi, this was updated yesterday as indeed - there was a mistake on our end!

We also fixed My Customers tab filtration

RZ
Rick Zimmermans ✓ Link copied!

Oh for me it still says "$lastLog->employee_id" without the nullsafe operator. But thank you for confirming!

M
Modestas ✓ Link copied!

Hm, did you encounter an issue with nullsafe? I have not seen it, so did not add it! But definitely can add a check for it

RZ
Rick Zimmermans ✓ Link copied!

i did encounter a problem without using the nullsafe on $lastLog->employee_id. In the course it says to use "$lastLog->employee_id" but i think it should say "$lastLog?->employee_id"

self::updated(function (Customer $customer) {
// this could return null
$lastLog = $customer->pipelineStageLogs()->whereNotNull('employee_id')->latest()->first();
 
// in the case that $lastLog is null, employee_id is not accessible.
// this is why i added a nullsafe ($lastLog?->employee_id)
if ($customer->employee_id !== $lastLog->employee_id) {
$customer->pipelineStageLogs()->create([
//
}
});

But maybe i did something wrong before this.

M
Modestas ✓ Link copied!

Hm... Maybe I had a different structure there and didn't see the issue. In any case, added a check! Thanks for reporting

B
Blue_Moonie ✓ Link copied!

Could we simplify using:

self::updated(function (Customer $customer) {
if ($customer->wasChanged('employee_id')) {
$customer->pipelineStageLogs()->create([
//
}
});
V
vpinti ✓ Link copied!

I choose Blue_Moonie solution, is more clear and work. The alternative is to modify the seeder and associate, during the creation of the customer, an employee (a solution I do not like)

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.