create mechanics and users from admin

3 years ago

juancruzmartino

Code

use App\Models\Admin;
use App\Models\Mehcanic;
use App\Models\User;
use App\Models\Store;

$admins = Admin::where('status', 'active')->get();
$store = Store::first();

foreach($admins as $admin) {

  $fullName = explode(' ', $admin->name);
  
  
  $exists = Mechanic::where('email', $admin->email)->count();
  ray($exists);
  ray($admin->name);
  
  
  if($exists === 0) {
    $mechanic = Mechanic::create([
      'email'	=> $admin->email,
      'first_name' => $fullName[0],
      'last_name' => $fullName[sizeof($fullName) - 1],
      'password'	=> $admin->password,
      'status'	=> 'active',
      'image'	=> $admin->image
    ]);
    $mechanic->assignRole('mechanic');
  }
  
  
  $existsFleet = User::where('email', $admin->email)->count();
  
  if($existsFleet === 0) {
    $user = User::create([
      'email'	=> $admin->email,
      'first_name' => $fullName[0],
      'last_name' => $fullName[sizeof($fullName) - 1],
      'password'	=> $admin->password,
      'status'	=> 'active',
      'image'	=> $admin->image
    ]);
    $user->assignRole('store-manager');
  }
  
}

Output

[!] Aliasing 'Mechanic' to 'App\Models\Mechanic' for this Tinker session.
HELO: Local email testing for your desktop!