Skip to content

Commit 42bf790

Browse files
Improved Seeders and models to support more laravel 12.x syntax
1 parent 6e7aa0c commit 42bf790

18 files changed

+202
-112
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ password - 12345678
1111
## Requirements:
1212
- Laravel `7.x` | `9.7` | `11.x` | `12.x`
1313
- Spatie role permission package `^6.4`
14+
- PHPUnit test package `^11.x`
1415

1516
## Versions:
1617
- Laravel `7.x` & PHP -`7.x`
@@ -23,7 +24,7 @@ password - 12345678
2324
- Laravel `11.x`
2425
- Tag - https://github.com/ManiruzzamanAkash/laravel-role/releases/tag/v11.x-main
2526

26-
- Laravel `12.x`
27+
- Laravel `12.x` & PHP >= `8.3`
2728
- Tag - https://github.com/ManiruzzamanAkash/laravel-role/releases/tag/Laravel12.x
2829

2930
## Project Setup

app/Http/Controllers/Auth/RegisterController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Providers\RouteServiceProvider;
7-
use App\User;
7+
use App\Models\User;
88
use Illuminate\Foundation\Auth\RegistersUsers;
99
use Illuminate\Support\Facades\Hash;
1010
use Illuminate\Support\Facades\Validator;
@@ -60,9 +60,9 @@ protected function validator(array $data)
6060
* Create a new user instance after a valid registration.
6161
*
6262
* @param array $data
63-
* @return \App\User
63+
* @return ?User
6464
*/
65-
protected function create(array $data)
65+
protected function create(array $data): ?User
6666
{
6767
return User::create([
6868
'name' => $data['name'],

app/Http/Controllers/Backend/RolesController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace App\Http\Controllers\Backend;
66

7-
use App\User;
7+
use App\Models\User;
88
use App\Http\Controllers\Controller;
99
use App\Http\Requests\RoleRequest;
1010
use Illuminate\Contracts\Support\Renderable;

app/Models/Admin.php

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace App\Models;
46

5-
use Illuminate\Contracts\Auth\MustVerifyEmail;
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
68
use Illuminate\Foundation\Auth\User as Authenticatable;
79
use Illuminate\Notifications\Notifiable;
810
use Illuminate\Support\Facades\DB;
11+
use Spatie\Permission\Models\Role;
912
use Spatie\Permission\Traits\HasRoles;
1013

1114
class Admin extends Authenticatable
1215
{
13-
use Notifiable, HasRoles;
16+
use Notifiable, HasRoles, HasFactory;
1417

1518
/**
1619
* Set the default guard for this model.
@@ -25,7 +28,9 @@ class Admin extends Authenticatable
2528
* @var array
2629
*/
2730
protected $fillable = [
28-
'name', 'email', 'password',
31+
'name',
32+
'email',
33+
'password',
2934
];
3035

3136
/**
@@ -34,7 +39,8 @@ class Admin extends Authenticatable
3439
* @var array
3540
*/
3641
protected $hidden = [
37-
'password', 'remember_token',
42+
'password',
43+
'remember_token',
3844
];
3945

4046
/**
@@ -48,31 +54,28 @@ class Admin extends Authenticatable
4854

4955
public static function getpermissionGroups()
5056
{
51-
$permission_groups = DB::table('permissions')
57+
return DB::table('permissions')
5258
->select('group_name as name')
5359
->groupBy('group_name')
5460
->get();
55-
return $permission_groups;
5661
}
5762

58-
public static function getpermissionsByGroupName($group_name)
63+
public static function getpermissionsByGroupName(string $group_name)
5964
{
60-
$permissions = DB::table('permissions')
65+
return DB::table('permissions')
6166
->select('name', 'id')
6267
->where('group_name', $group_name)
6368
->get();
64-
return $permissions;
6569
}
6670

67-
public static function roleHasPermissions($role, $permissions)
71+
public static function roleHasPermissions(Role $role, array $permissions): bool
6872
{
69-
$hasPermission = true;
7073
foreach ($permissions as $permission) {
7174
if (!$role->hasPermissionTo($permission->name)) {
72-
$hasPermission = false;
73-
return $hasPermission;
75+
return false;
7476
}
7577
}
76-
return $hasPermission;
78+
79+
return true; // ensure returning true if all permissions are granted
7780
}
7881
}

app/User.php app/Models/User.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?php
22

3-
namespace App;
3+
declare(strict_types=1);
4+
5+
namespace App\Models;
46

57
use Illuminate\Contracts\Auth\MustVerifyEmail;
8+
use Illuminate\Database\Eloquent\Factories\HasFactory;
69
use Illuminate\Foundation\Auth\User as Authenticatable;
710
use Illuminate\Notifications\Notifiable;
811
use Illuminate\Support\Facades\DB;
912
use Spatie\Permission\Traits\HasRoles;
1013

1114
class User extends Authenticatable
1215
{
13-
use Notifiable, HasRoles;
16+
use Notifiable, HasRoles, HasFactory;
1417

1518
/**
1619
* The attributes that are mass assignable.

composer.json

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
],
99
"license": "MIT",
1010
"require": {
11-
"php": "^8.2",
11+
"php": "^8.3",
1212
"laravel/framework": "^12.0",
13-
"laravel/tinker": "^2.9",
13+
"laravel/tinker": "^2.10",
1414
"guzzlehttp/guzzle": "^7.2",
1515
"laravel/ui": "^4.5",
1616
"spatie/laravel-permission": "^6.4"
1717
},
1818
"require-dev": {
19-
"fakerphp/faker": "^1.23",
20-
"laravel/pint": "^1.13",
21-
"laravel/sail": "^1.26",
19+
"fakerphp/faker": "^1.24",
20+
"laravel/pint": "^1.21",
21+
"laravel/sail": "^1.41",
2222
"mockery/mockery": "^1.6",
23-
"nunomaduro/collision": "^8.0",
24-
"phpunit/phpunit": "^11.0",
25-
"spatie/laravel-ignition": "^2.4"
23+
"nunomaduro/collision": "^8.6",
24+
"phpunit/phpunit": "^11.5.3",
25+
"spatie/laravel-ignition": "^2.9"
2626
},
2727
"config": {
2828
"optimize-autoloader": true,
@@ -36,10 +36,12 @@
3636
},
3737
"autoload": {
3838
"psr-4": {
39-
"App\\": "app/"
39+
"App\\": "app/",
40+
"Database\\Factories\\": "database/factories/",
41+
"Database\\Seeders\\": "database/seeders/"
4042
},
4143
"classmap": [
42-
"database/seeds",
44+
"database/seeders",
4345
"database/factories"
4446
]
4547
},

composer.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/auth.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
'providers' => [
8080
'users' => [
8181
'driver' => 'eloquent',
82-
'model' => App\User::class,
82+
'model' => App\Models\User::class,
8383
],
8484

8585
'admins' => [

database/factories/AdminFactory.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Database\Factories;
6+
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Illuminate\Support\Facades\Hash;
9+
use Illuminate\Support\Str;
10+
11+
/**
12+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Admin>
13+
*/
14+
class AdminFactory extends Factory
15+
{
16+
/**
17+
* The current password being used by the factory.
18+
*/
19+
protected static ?string $password;
20+
21+
/**
22+
* Define the model's default state.
23+
*
24+
* @return array<string, mixed>
25+
*/
26+
public function definition(): array
27+
{
28+
return [
29+
'name' => fake()->name(),
30+
'email' => fake()->unique()->safeEmail(),
31+
'username' => fake()->unique()->userName(),
32+
'email_verified_at' => now(),
33+
'password' => static::$password ??= Hash::make('password'),
34+
'remember_token' => Str::random(10),
35+
];
36+
}
37+
38+
/**
39+
* Indicate that the model's email address should be unverified.
40+
*/
41+
public function unverified(): static
42+
{
43+
return $this->state(fn(array $attributes) => [
44+
'email_verified_at' => null,
45+
]);
46+
}
47+
}

database/factories/UserFactory.php

+40-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
<?php
22

3-
/** @var \Illuminate\Database\Eloquent\Factory $factory */
3+
declare(strict_types=1);
44

5-
use App\User;
6-
use Faker\Generator as Faker;
5+
namespace Database\Factories;
6+
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Illuminate\Support\Facades\Hash;
79
use Illuminate\Support\Str;
810

9-
/*
10-
|--------------------------------------------------------------------------
11-
| Model Factories
12-
|--------------------------------------------------------------------------
13-
|
14-
| This directory should contain each of the model factory definitions for
15-
| your application. Factories provide a convenient way to generate new
16-
| model instances for testing / seeding your application's database.
17-
|
18-
*/
11+
/**
12+
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
13+
*/
14+
class UserFactory extends Factory
15+
{
16+
/**
17+
* The current password being used by the factory.
18+
*/
19+
protected static ?string $password;
20+
21+
/**
22+
* Define the model's default state.
23+
*
24+
* @return array<string, mixed>
25+
*/
26+
public function definition(): array
27+
{
28+
return [
29+
'name' => fake()->name(),
30+
'email' => fake()->unique()->safeEmail(),
31+
'email_verified_at' => now(),
32+
'password' => static::$password ??= Hash::make('password'),
33+
'remember_token' => Str::random(10),
34+
];
35+
}
1936

20-
$factory->define(User::class, function (Faker $faker) {
21-
return [
22-
'name' => $faker->name,
23-
'email' => $faker->unique()->safeEmail,
24-
'email_verified_at' => now(),
25-
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
26-
'remember_token' => Str::random(10),
27-
];
28-
});
37+
/**
38+
* Indicate that the model's email address should be unverified.
39+
*/
40+
public function unverified(): static
41+
{
42+
return $this->state(fn(array $attributes) => [
43+
'email_verified_at' => null,
44+
]);
45+
}
46+
}

database/seeders/AdminSeeder.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Database\Seeders;
6+
7+
use App\Models\Admin;
8+
use DB;
9+
use Illuminate\Database\Seeder;
10+
use Illuminate\Support\Facades\Hash;
11+
12+
class AdminSeeder extends Seeder
13+
{
14+
/**
15+
* Run the database seeds.
16+
*
17+
* @return void
18+
*/
19+
public function run(): void
20+
{
21+
$admin =
22+
[
23+
'name' => 'Super Admin',
24+
'email' => 'superadmin@example.com',
25+
'username' => 'superadmin',
26+
'password' => Hash::make('12345678'),
27+
];
28+
29+
Admin::create($admin);
30+
31+
// Run factory to create additional admins with unique details.
32+
Admin::factory()->count(10)->create();
33+
$this->command->info('Admins table seeded with 11 admins!');
34+
}
35+
}

database/seeds/DatabaseSeeder.php database/seeders/DatabaseSeeder.php

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php
22

3+
declare(strict_types=1);
4+
5+
namespace Database\Seeders;
6+
37
use Illuminate\Database\Seeder;
48

59
class DatabaseSeeder extends Seeder

0 commit comments

Comments
 (0)