使用laravel 5.2 socialite将Facebook用户与我们的应用程序集成

时间:2016-06-24 03:29:48

标签: php mysql facebook laravel migration

实际上,我正在按照http://blog.damirmiladinov.com/laravel/laravel-5.2-socialite-facebook-login.html#.V2yhIbjJvIU

的说明进行操作

我卡住的步骤是

将以下字段添加到迁移中:

Schema::table('social_accounts', function (Blueprint $table) {
    $table->integer('user_id');
    $table->string('provider_user_id');
    $table->string('provider');
    $table->timestamps();
});

运行迁移

  

php artisan migrate:refresh

运行此命令时出现

错误:

  

[照亮\数据库\ QueryException]

     

SQLSTATE [42S01]:基表或视图已存在:1050表   “社会   _acco已经存在(SQL:create table social_accountsid int un signe d not null auto_increment primary key,   created_at timestamp null,u pdate d_at timestamp null)默认值   字符集utf8 collat​​e utf8_unicode_ ci)

     

[PDOException]

     

SQLSTATE [42S01]:基表或视图已存在:1050表   “社会   _acco已经存在

以下是我在点击fblogin后在localhost上的错误。

  

Connection.php第713行中的QueryException:SQLSTATE [42S22]:列不是   发现:1054'where子句'中的未知列'provider'(SQL:select *   来自social_accounts provider = facebook和。{   provider_user_id = 1130902766955428限制1)

我的迁移文件:

  <?php

  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;

  class CreateSocialAccountsTable extends Migration
 {
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('social_accounts', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    }); 

    Schema::table('social_accounts', function (Blueprint $table) {
    $table->integer('user_id');
    $table->string('provider_user_id');
    $table->string('provider');
    $table->timestamps();
});


}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('social_accounts');
}
 }

我的控制器文件:

 <?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;

   use App\Http\Requests;

   use App\Http\Controllers\Controller;

    use App\SocialAccountService;

    use Socialite;

   class SocialAuthController extends Controller
  {
  public function redirect()
 {
    return Socialite::driver('facebook')->redirect();   
}   

public function callback(SocialAccountService $service)
{


  $user = $service->createOrGetUser(Socialite::driver('facebook')->user());

    auth()->login($user);

    return redirect()->to('/home');
             }
        }

我的app文件夹中的文件:

SocialAccount.php

       <?php

        namespace App;

        use Illuminate\Database\Eloquent\Model;

        class SocialAccount extends Model
         {
     protected $fillable = ['user_id', 'provider_user_id', 'provider'];

      public function user()
   {
    return $this->belongsTo(User::class);
    }
     }

SocialAccountservice.php

           <?php

             namespace App;

              use Laravel\Socialite\Contracts\User as ProviderUser;

              class SocialAccountService
             {
          public function createOrGetUser(ProviderUser $providerUser)
             {
           $account = SocialAccount::whereProvider('facebook')
        ->whereProviderUserId($providerUser->getId())
        ->first();

    if ($account) {
        return $account->user();
    } else {

        $account = new SocialAccount([
            'provider_user_id' => $providerUser->getId(),
            'provider' => 'facebook'
        ]);

        $user = User::whereEmail($providerUser->getEmail())->first();

        if (!$user) {

            $user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
            ]);
        }

        $account->user()->associate($user);
        $account->save();

        return $user;

    }

}
    }

2 个答案:

答案 0 :(得分:1)

数据库中已经有一个名为'social_accounts'的表。首先删除该表&amp;然后尝试运行迁移。

答案 1 :(得分:0)

[解决]

第1步:从config / app.php

中删除社交网站引用

第2步:运行composer update

第3步:然后运行composer dump-autoload

步骤4:然后将社交名录引用添加到config / app.php

步骤5:最后,运行composer dump-autoload