argument('siteid'))); $so = Supplier::where('name',$this->argument('supplier'))->singleOrFail(); foreach ($so->API($this->option('forceprod'))->getCustomers(['fetchall'=>true]) as $customer) { // Check if we have this customer already (by ID) if ($so->users->where('pivot.id',$customer->id)->count()) { $this->info(sprintf('User already linked (%s:%s)',$customer->id,$customer->email)); } elseif ($x=User::where('email',$customer->email)->single()) { //dump($x->suppliers->first()); if ($x->suppliers->count()) { $this->alert(sprintf('User [%d:%s] already linked to this supplier with ID (%s)',$customer->id,$customer->email,$x->suppliers->first()->pivot->id)); } else { $this->warn(sprintf('User [%d:%s] has same email (%s:%s) - Linked',$x->id,$x->email,$customer->id,$customer->email)); $so->users()->syncWithoutDetaching([ $x->id => [ 'id'=>$customer->id, 'site_id'=>$x->site_id, // @todo See if we can have this handled automatically 'created_at'=>Carbon::create($customer->date_added), ] ]); } } else { $this->error(sprintf('User doesnt exist with email (%s)',$customer->email)); } } } }