Commit ad45c520 authored by ZeMKI's avatar ZeMKI
Browse files

New Command User delete

* now you can delete users with the command user:delete, entering either email or id. You can also choose id or email.
* delete profile table and seeder, not needed.
parent a73f5bdd
<?php
namespace App\Console\Commands;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class DeleteUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:delete';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete a user from the database. He will not be able to log in anymore!';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$info = $this->choice('Email or ID?', ['email', 'id']);
if ($info === "email") {
$email = $this->ask('Enter email');
try {
$user = User::where('email', '=', $email)->firstOrFail();
} catch (ModelNotFoundException $e) {
$this->warn("user not found!");
return false;
}
}else{
$id = $this->ask('Enter id');
try {
$user = User::where('id', '=', $id)->firstOrFail();
} catch (ModelNotFoundException $e) {
$this->warn("user not found!");
return false;
}
}
if($this->confirm("ARE YOU SURE YOU WANT TO DELETE THIS USER? ".$user->email, false))
{
$whichDelete = $this->choice('Soft or Force deletion?', ['Soft', 'Force']);
if($whichDelete == "force"){
$user->forceDelete();
}else{
$user->delete();
}
}
}
}
......@@ -14,7 +14,8 @@ class Kernel extends ConsoleKernel
*/
protected $commands = [
Commands\CreateUserCommand::class,
Commands\DeleteClosedStudies::class
Commands\DeleteClosedStudies::class,
Commands\DeleteUserCommand::class
];
/**
......
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersProfilesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users_profiles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->references('id')->on('users')->onDelete('cascade');
$table->string('name', 255);
$table->string('address', 255);
$table->date('birthday');
$table->string('phonenumber1', 190);
$table->string('phonenumber2', 190);
$table->string('workaddress', 255);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users_profiles');
}
}
......@@ -12,10 +12,8 @@ class DatabaseSeeder extends Seeder
public function run()
{
$this->call(LaratrustSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(UserRoleTableSeeder::class);
$this->call(UsersProfilesSeeder::class);
$this->call(SortingTableSeeder::class);
}
}
......@@ -13,8 +13,8 @@ class SortingTableSeeder extends Seeder
{
// admin with all db2_column_privileges(connection)
DB::table('sortings')->insert([
'name' => "first",
'description' => "a"
'name' => "Circle Sorting",
'description' => "First circle sorting"
]);
}
}
<?php
use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use Illuminate\Support\Facades\DB;
use App\User;
class UsersProfilesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker::create();
foreach (User::all() as $user) {
DB::table('users_profiles')->insert([
'name' => $faker->name,
'user_id' => $user->id,
'address' => $faker->address,
'workaddress' => $faker->address,
'birthday' => $faker->date,
'phonenumber1' => $faker->phoneNumber,
'phonenumber2' => $faker->phoneNumber
]);
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment