Skip to content

Commit 378f1d4

Browse files
Create roles Index.vue component and its controller and resource
1 parent 3026f51 commit 378f1d4

File tree

15 files changed

+442
-111
lines changed

15 files changed

+442
-111
lines changed

app/Http/Controllers/RoleController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Http\Resources\RoleResource;
6+
use App\Models\Role;
57
use Illuminate\Http\Request;
8+
use Inertia\Inertia;
69

710
class RoleController extends Controller
811
{
@@ -11,7 +14,9 @@ class RoleController extends Controller
1114
*/
1215
public function index()
1316
{
14-
//
17+
return Inertia::render('Roles/Index', [
18+
'roles' => RoleResource::collection(Role::all()),
19+
]);
1520
}
1621

1722
/**

app/Http/Controllers/UserController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UserController extends Controller
1414
public function index()
1515
{
1616
return Inertia::render('Users/Index', [
17-
'users' => User::all()
17+
'users' => User::with('role')->get()
1818
]);
1919
}
2020

@@ -65,4 +65,4 @@ public function destroy(string $id)
6565
{
6666
//
6767
}
68-
}
68+
}

app/Http/Middleware/HandleInertiaRequests.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public function version(Request $request): ?string
3636
*/
3737
public function share(Request $request): array
3838
{
39-
return array_merge(parent::share($request), [
40-
//
41-
]);
39+
return array_merge(parent::share($request), []);
4240
}
4341
}

app/Http/Resources/RoleResource.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use App\Models\User;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class RoleResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
15+
*/
16+
public function toArray($request)
17+
{
18+
return [
19+
'id' => $this->id,
20+
'name' => $this->name,
21+
'count' => User::where('role_id', $this->id)->count(),
22+
'formatted_created_at' => $this->created_at->format('d M Y'),
23+
];
24+
}
25+
}

app/Models/Role.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Role extends Model
9+
{
10+
use HasFactory;
11+
protected $guarded = [];
12+
13+
public function rox()
14+
{
15+
return User::where('role_id', $this->id)->count();
16+
}
17+
}

app/Models/User.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,17 @@ class User extends Authenticatable
1818
use Notifiable;
1919
use TwoFactorAuthenticatable;
2020

21+
protected $guarded = [];
2122
/**
2223
* The attributes that are mass assignable.
2324
*
2425
* @var array<int, string>
2526
*/
26-
protected $fillable = [
27-
'name',
28-
'email',
29-
'password',
30-
];
27+
// protected $fillable = [
28+
// 'name',
29+
// 'email',
30+
// 'password',
31+
// ];
3132

3233
/**
3334
* The attributes that should be hidden for serialization.
@@ -55,7 +56,23 @@ class User extends Authenticatable
5556
*
5657
* @var array<int, string>
5758
*/
58-
protected $appends = [
59-
'profile_photo_url',
60-
];
59+
// protected $appends = [
60+
// 'profile_photo_url',
61+
// ];
62+
63+
public function getFormattedCreatedAtAttribute()
64+
{
65+
return $this->created_at->format('d M Y');
66+
}
67+
68+
public function role()
69+
{
70+
return $this->belongsTo(Role::class);
71+
}
72+
73+
public function isAdmin()
74+
{
75+
// return $this->role()->id == 1;
76+
return $this->role->id == 1;
77+
}
6178
}

app/Providers/AppServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Providers;
44

5+
use Illuminate\Http\Resources\Json\JsonResource;
56
use Illuminate\Support\ServiceProvider;
67

78
class AppServiceProvider extends ServiceProvider
@@ -19,6 +20,5 @@ public function register(): void
1920
*/
2021
public function boot(): void
2122
{
22-
//
2323
}
2424
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use App\Models\Role as ModelsRole;
4+
use Carbon\Carbon;
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
/**
12+
* Run the migrations.
13+
*/
14+
public function up(): void
15+
{
16+
Schema::create('roles', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name', 255);
19+
$table->timestamps();
20+
});
21+
22+
ModelsRole::insert([
23+
['name' => 'Administrator', 'created_at' => Carbon::now()],
24+
['name' => 'Subscriber', 'created_at' => Carbon::now()],
25+
['name' => 'Guest', 'created_at' => Carbon::now()]
26+
]);
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*/
32+
public function down(): void
33+
{
34+
Schema::dropIfExists('roles');
35+
}
36+
};

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use App\Models\User;
34
use Illuminate\Database\Migrations\Migration;
45
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Support\Facades\Schema;
@@ -20,8 +21,19 @@ public function up(): void
2021
$table->rememberToken();
2122
$table->foreignId('current_team_id')->nullable();
2223
$table->string('profile_photo_path', 2048)->nullable();
24+
$table->bigInteger('role_id')->unsigned()->index()->nullable();
25+
$table->foreign('role_id')->references('id')->on('roles')->cascadeOnDelete();
2326
$table->timestamps();
2427
});
28+
29+
User::insert([
30+
'name' => 'Admin',
31+
'email' => 'admin@gmail.com',
32+
'password' => bcrypt('nekalozinka'),
33+
'created_at' => now(),
34+
'updated_at' => now(),
35+
'role_id' => 1,
36+
]);
2537
}
2638

2739
/**

0 commit comments

Comments
 (0)