SHARE
TWEET

buat user berdasar data mahasiswa

a guest Dec 6th, 2016 250 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Stmik\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6. use Stmik\Mahasiswa;
  7. use Stmik\User;
  8.  
  9. class BikinUserMahasiswaCmd extends Command
  10. {
  11.     /**
  12.      * The name and signature of the console command.
  13.      *
  14.      * @var string
  15.      */
  16.     protected $signature = 'siakad:users';
  17.  
  18.     /**
  19.      * The console command description.
  20.      *
  21.      * @var string
  22.      */
  23.     protected $description = 'Reset semua user mahasiswa, bikin user dari data mahasiswa';
  24.  
  25.     /**
  26.      * Create a new command instance.
  27.      *
  28.      * @return void
  29.      */
  30.     public function __construct()
  31.     {
  32.         parent::__construct();
  33.     }
  34.  
  35.     /**
  36.      * Execute the console command.
  37.      *
  38.      * @return mixed
  39.      */
  40.     public function handle()
  41.     {
  42.         $this->info('Setting akan dilakukan, reset akan dilaksanakan. Awas data hilang, ingat backup!');
  43.         if(!$this->confirm('Lanjutkan?')) {
  44.             $this->info('Dibatalkan!');
  45.             exit();
  46.         }
  47.         //lanjutkan
  48.         \DB::beginTransaction();
  49.         try {
  50.             $chunks = 0;
  51.             Mahasiswa::with('user')->chunk(100, function($mahasiswa) use(&$chunks) {
  52.                 $this->info('Melakukan proses data mahasiswa bagian ke ' . (++$chunks) );
  53.                 /** @var Mahasiswa $m */
  54.                 $m = null;
  55.                 foreach($mahasiswa as $m) {
  56.                     $user = $m->user; // ambil data user
  57.                     if($user === null) { // belum punya user?
  58.                         // user baru
  59.                         $user = new User();
  60.                         } else {
  61.                             continue; // jangan rubah yang sudah dimasukkan!
  62.                     }
  63.                     // set password dengan tanggal lahir!
  64.                     $user->password = \Hash::make($m->tgl_lahir);
  65.                     // set nomor induk sebagai username
  66.                     $user->name = $m->nomor_induk;
  67.                     // email kosong dulu!
  68.                     $user->email = $m->nomor_induk.'@mohon-ubah-saya.com';
  69.                     $user->save();
  70.                     // ingat ini polymorphic maka ... set ke dalam relasi milik modelEmpunyaUser
  71.                     $m->user()->save($user);
  72.                     // yeah untuk Role default ...?
  73.                     $user->assignRole('mahasiswa');
  74.                 }
  75.             });
  76.             \DB::commit();
  77.             $this->info('Selesai ...');
  78.         } catch (\Exception $e) {
  79.             \DB::rollBack();
  80.             \Log::alert("Bad Happen:" . $e->getMessage() . "\n" . $e->getTraceAsString(), []);
  81.             $this->error("OH FUCK : ".$e->getMessage());
  82.         }
  83.     }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top