Skip to content

Commit

Permalink
Creation of the migration, model, seeder, factory to record guard's r…
Browse files Browse the repository at this point in the history
…eport
  • Loading branch information
JosueOb committed Oct 8, 2021
1 parent ccefe2d commit d6a7b9a
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/Models/Report.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Models;

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

class Report extends Model
{
use HasFactory;

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


/**
* Relationships
*
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
6 changes: 6 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
Expand Down Expand Up @@ -58,4 +59,9 @@ public function wards(): BelongsToMany
return $this->belongsToMany(Ward::class)
->withPivotValue();
}

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

namespace Database\Factories;

use App\Models\Report;
use App\Models\Role;
use Illuminate\Database\Eloquent\Factories\Factory;

class ReportFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Report::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$guard_role = Role::where('name', 'guard')->first();
return [
'title'=> $this->faker->title,
'description'=> $this->faker->text(255),
'user_id' => $guard_role->users->random()->id,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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

class AddUserIdColumnToReportsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('reports', function (Blueprint $table) {
$table->foreignId('user_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('reports', function (Blueprint $table) {
$table->dropForeign(['user_id']);
});
}
}
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function run(): void
RoleSeeder::class,
UserSeeder::class,
WardSeeder::class,
ReportSeeder::class,
]);
}
}
22 changes: 22 additions & 0 deletions database/seeders/ReportSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Database\Seeders;

use App\Models\Report;
use App\Models\Role;
use Illuminate\Database\Seeder;

class ReportSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Report::factory()
->count(25)
->create();
}
}

0 comments on commit d6a7b9a

Please sign in to comment.