Skip to content

Commit

Permalink
Created Seeder for Many Students
Browse files Browse the repository at this point in the history
  • Loading branch information
4jean committed Oct 21, 2020
1 parent 113e5bf commit 74a2bc6
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 60 deletions.
6 changes: 6 additions & 0 deletions app/Helpers/Qs.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ public static function getStaff($remove=[])
return $remove ? array_values(array_diff($data, $remove)) : $data;
}

public static function getAllUserTypes($remove=[])
{
$data = ['super_admin', 'admin', 'teacher', 'accountant', 'librarian', 'student', 'parent'];
return $remove ? array_values(array_diff($data, $remove)) : $data;
}

// Check if User is Head of Super Admins (Untouchable)
public static function headSA(int $user_id)
{
Expand Down
3 changes: 3 additions & 0 deletions app/Models/StudentRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use App\User;
use Eloquent;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class StudentRecord extends Eloquent
{
use HasFactory;

protected $fillable = [
'session', 'user_id', 'my_class_id', 'section_id', 'my_parent_id', 'dorm_id', 'dorm_room_no', 'adm_no', 'year_admitted', 'wd', 'wd_date', 'grad', 'grad_date', 'house', 'age'
];
Expand Down
3 changes: 2 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
use App\Models\StaffRecord;
use App\Models\State;
use App\Models\StudentRecord;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;
use HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
Expand Down
17 changes: 12 additions & 5 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Database\Factories;

use App\Helpers\Qs;
use App\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class UserFactory extends Factory
Expand All @@ -22,12 +24,17 @@ class UserFactory extends Factory
*/
public function definition()
{
// Create random User Type
$user_type = Qs::getStaff(['super_admin', 'librarian'])[rand(0,2)];

return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
'name' => $this->faker->name,
'email' => $this->faker->safeEmail,
'username' => $this->faker->userName,
'password' => Hash::make($user_type),
'user_type' => $user_type,
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
];
}
}
3 changes: 2 additions & 1 deletion database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use App\Helpers\Qs;

class CreateUsersTable extends Migration
{
Expand All @@ -22,7 +23,7 @@ public function up()
$table->string('user_type');
$table->string('dob')->nullable();
$table->string('gender')->nullable();
$table->string('photo')->nullable();
$table->string('photo')->default(Qs::getDefaultUserImage());
$table->string('phone')->nullable();
$table->string('phone2')->nullable();
$table->unsignedInteger('bg_id')->nullable();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function up()
Schema::create('my_classes', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 100);
$table->unsignedInteger('class_type_id')->nullable();
$table->unsignedInteger('class_type_id');
$table->timestamps();
});

Expand Down
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public function run()
$this->call(LgasTableSeeder::class);
$this->call(SettingsTableSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(StudentRecordsTableSeeder::class);
}
}
6 changes: 5 additions & 1 deletion database/seeders/MyClassesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ public function run()
$ct = ClassType::pluck('id')->all();

$data = [
['name' => 'JSS 1', 'class_type_id' => $ct[4]],
['name' => 'Nursery 1', 'class_type_id' => $ct[2]],
['name' => 'Nursery 2', 'class_type_id' => $ct[2]],
['name' => 'Nursery 3', 'class_type_id' => $ct[2]],
['name' => 'Primary 1', 'class_type_id' => $ct[3]],
['name' => 'Primary 2', 'class_type_id' => $ct[3]],
['name' => 'JSS 2', 'class_type_id' => $ct[4]],
['name' => 'JSS 3', 'class_type_id' => $ct[4]],
['name' => 'SSS 1', 'class_type_id' => $ct[5]],
Expand Down
1 change: 1 addition & 0 deletions database/seeders/UserTypesTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function run()
['title' => 'teacher', 'name' => 'Teacher', 'level' => 3],
['title' => 'admin', 'name' => 'Admin', 'level' => 2],
['title' => 'super_admin', 'name' => 'Super Admin', 'level' => 1],
// ['title' => 'librarian', 'name' => 'librarian', 'level' => 6],
];
DB::table('user_types')->insert($data);
}
Expand Down
89 changes: 38 additions & 51 deletions database/seeders/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class UsersTableSeeder extends Seeder
{

/**
* Run the database seeds.
*
Expand All @@ -18,98 +19,84 @@ public function run()
{
DB::table('users')->delete();

$user_type = ['admin', 'student', 'teacher', 'parent', 'super_admin'];

$this->newUsers($user_type);
$this->multiUsers($user_type);
$this->createNewUsers();
$this->createManyUsers( 3);
}

protected function newUsers($ut)
protected function createNewUsers()
{
// Default user password
$password = Hash::make('cj');
$password = Hash::make('cj'); // Default user password

$d = [

['name' => 'CJ Inspired',
'email' => 'cj@cj.com',
'username' => 'cj',
'password' => $password,
'user_type' => $ut[4],
'user_type' => 'super_admin',
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
'photo' => Qs::getDefaultUserImage(),
],

['name' => 'Admin KORA',
'email' => $ut[0].'@'.$ut[0].'.com',
'email' => 'admin@admin.com',
'password' => $password,
'user_type' => $ut[0],
'username' => $ut[0],
'user_type' => 'admin',
'username' => 'admin',
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
'photo' => Qs::getDefaultUserImage(),
],

['name' => 'Teacher Chike',
'email' => $ut[2].'@'.$ut[2].'.com',
'user_type' => $ut[2],
'username' => $ut[2],
'email' => 'teacher@teacher.com',
'user_type' => 'teacher',
'username' => 'teacher',
'password' => $password,
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
'photo' => Qs::getDefaultUserImage(),
],

['name' => 'Parent Kaba',
'email' => $ut[3].'@'.$ut[3].'.com',
'user_type' => $ut[3],
'username' => $ut[3],
'email' => 'parent@parent.com',
'user_type' => 'parent',
'username' => 'parent',
'password' => $password,
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
],

['name' => 'Accountant Jeff',
'email' => 'accountant@accountant.com',
'user_type' => 'accountant',
'username' => 'accountant',
'password' => $password,
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
'photo' => Qs::getDefaultUserImage(),
],
];
DB::table('users')->insert($d);
}

protected function multiUsers($ut)
protected function createManyUsers(int $count)
{
$data = [];
$user_type = Qs::getAllUserTypes(['super_admin', 'librarian', 'student']);

for($i=1; $i<2; $i++){
for($i = 1; $i <= $count; $i++){

/*Create Student */
/* $data[] = ['name' => $ut[1].' '.$i,
'email' => $ut[1].$i.'@'.$ut[1].'.com',
'user_type' => $ut[1],
'password' => Hash::make($ut[1]),
'code' => Str::random(10),
'remember_token' => Str::random(10),
'photo' => Qs::getDefaultUserImage(),
];*/

/*Create Teachers */
$data[] = ['name' => $ut[2].' '.$i,
'email' => $ut[2].$i.'@'.$ut[2].'.com',
'user_type' => $ut[2],
'password' => Hash::make($ut[2]),
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
'photo' => Qs::getDefaultUserImage(),
];
foreach ($user_type as $k => $ut){

/*Create Parents Users*/
$data[] = ['name' => ucfirst($user_type[$k]).' '.$i,
'email' => $user_type[$k].$i.'@'.$user_type[$k].'.com',
'user_type' => $user_type[$k],
'username' => $user_type[$k].$i,
'password' => Hash::make($user_type[$k]),
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
];

}

$data[] = ['name' => $ut[3].' '.$i,
'email' => $ut[3].$i.'@'.$ut[3].'.com',
'user_type' => $ut[3],
'password' => Hash::make($ut[3]),
'code' => strtoupper(Str::random(10)),
'remember_token' => Str::random(10),
'photo' => Qs::getDefaultUserImage(),
];
}

DB::table('users')->insert($data);
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ After seeding. Login details as follows:
| Admin | admin | admin@admin.com | cj |
| Teacher | teacher | teacher@teacher.com | cj |
| Parent | parent | parent@parent.com | cj |
| Accountant | accountant | accountant@accountant.com | cj |
| Student | student | student@student.com | cj |

#### **FUNCTIONS OF ACCOUNTS**

Expand Down

0 comments on commit 74a2bc6

Please sign in to comment.