Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions app/Http/Queries/UserIndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UserIndexQuery extends QueryBuilder
{
public function __construct(Request $request)
{
parent::__construct(User::withTrashed(), $request);
parent::__construct(User::withTrashed()->with(['roles', 'type']), $request);

$this
->allowedFilters([
Expand Down Expand Up @@ -68,7 +68,8 @@ public function paginate(
$columns = ['*'],
$pageName = 'page',
$page = null
): \Illuminate\Contracts\Pagination\LengthAwarePaginator {
): \Illuminate\Contracts\Pagination\LengthAwarePaginator
{
$paginator = parent::paginate($perPage, $columns, $pageName, $page);

$paginator->appends(request()->query());
Expand All @@ -81,21 +82,36 @@ public function table(): Table
return Table::make($this->paginate())
->columns([
Columns\TextColumn::make('name')
->sortable()
->label('Full name'),
->label('Full name')
->sortable(),

Columns\TagsColumn::make('roles.name')
->label('Role'),

Columns\TextColumn::make('type.name')
->label('Types'),

Columns\TextColumn::make('books.title')
->label('Books'),

Columns\TextColumn::make('email_verified_at')
->label('Email verified at')
->dateTime()
->sortable()
->label('Email verified at'),
->sortable(),

Columns\BadgeColumn::make('language_developer')
->label('Programming languages')
->sortable()
->label('Programming languages'),
->colors([
'primary' => 'c-sharp',
'danger' => 'dart',
'warning' => 'javascript',
'success' => 'python',
]),

Columns\BooleanColumn::make('deleted_at')
->getStateUsing(fn(Columns\BooleanColumn $column, User $record) => !$record->trashed())
->label('Deleted at'),
->label('Deleted at')
->getStateUsing(fn(Columns\BooleanColumn $column, User $record) => !$record->trashed()),
]);
}
}
21 changes: 21 additions & 0 deletions app/Models/Book.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
use HasFactory;

protected $fillable = [
'title',
'total_pages'
];

public function user()
{
return $this->BelongsTo(User::class);
}
}
21 changes: 21 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
use HasFactory;

protected $fillable = [
'name',
'description'
];

public function users()
{
return $this->belongsToMany(User::class, 'role_user');
}
}
21 changes: 21 additions & 0 deletions app/Models/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Type extends Model
{
use HasFactory;

protected $fillable = [
'name',
'description'
];

public function users()
{
return $this->hasMany(User::class);
}
}
30 changes: 15 additions & 15 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,33 @@ class User extends Authenticatable
{
use HasFactory, Notifiable, SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];

public function roles()
{
return $this->belongsToMany(Role::class, 'role_user');
}

public function type()
{
return $this->belongsTo(Type::class);
}

public function books()
{
return $this->hasMany(Book::class);
}
}
24 changes: 24 additions & 0 deletions database/factories/BookFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Database\Factories;

use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class BookFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [

'user_id' => User::all()->random()->id,
'title' => $this->faker->colorName(),
'total_pages' => $this->faker->numberBetween(1,200),
];
}
}
21 changes: 21 additions & 0 deletions database/factories/RoleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class RoleFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->colorName(),
'description' => $this->faker->word(),
];
}
}
21 changes: 21 additions & 0 deletions database/factories/TypeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

class TypeFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->colorName(),
'description' => $this->faker->word(),
];
}
}
4 changes: 4 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Database\Factories;

use App\Models\Book;
use App\Models\Role;
use App\Models\Type;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
Expand All @@ -24,6 +27,7 @@ public function definition(): array
{
return [
'name' => $this->faker->name(),
'type_id' => Type::all()->random()->id,
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => $this->faker->dateTimeBetween('-2 years'),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
Expand Down
23 changes: 23 additions & 0 deletions database/migrations/2022_04_07_130952_create_roles_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRolesTable extends Migration
{
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('roles');
}
}
23 changes: 23 additions & 0 deletions database/migrations/2022_04_07_161515_create_types_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTypesTable extends Migration
{
public function up()
{
Schema::create('types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('types');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateAddTypeToUsersTable extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreignId('type_id')->constrained('types');
});
}

public function down()
{
Schema::table('users', function (Blueprint $table){
$table->foreignId('type_id')->constrained('types');
});
}
}
29 changes: 29 additions & 0 deletions database/migrations/2022_04_07_184212_create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->foreignId('type_id')->constrained('types');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('language_developer')->nullable();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}

public function down()
{
Schema::dropIfExists('users');
}
}
21 changes: 21 additions & 0 deletions database/migrations/2022_04_07_184509_create_role_user_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRoleUserTable extends Migration
{
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->foreignId('user_id')->constrained('users');
$table->foreignId('role_id')->constrained('roles');
});
}

public function down()
{
Schema::dropIfExists('role_user');
}
}
Loading