/** * @param $platform * @param $openId * @param $platformInfo */ public static function createWithPlatform($platform, $openId, $platformInfo) { \DB::transaction(function () use($platform, $openId, $platformInfo) { // create user $user = self::create(); // create user info $userInfo = new UserInfo(); $userInfo->nickname = 'MM' . substr($openId, 0, 6); $userInfo->user()->associate($user); $userInfo->save(); // create user token $userToken = new UserToken(); $userToken->platform = $platform; $userToken->open_id = $openId; $userToken->platform_info = $platformInfo; $userToken->user()->associate($user); $userToken->save(); }); }
public function findOrCreateWithPlatform($platform, $openId, $platformInfo) { $count = UserToken::wherePlatform($platform)->whereOpenId($openId)->count('user_id'); if (!$count) { User::createWithPlatform($platform, $openId, $platformInfo); } return User::with('userInfo', 'userToken')->join('user_tokens', function ($join) use($platform, $openId) { $join->on('users.id', '=', 'user_tokens.user_id')->where('user_tokens.platform', '=', $platform)->where('user_tokens.open_id', '=', $openId)->whereNotNull('users.id')->whereNotNull('user_tokens.user_id'); })->first(['users.*']); }