Foks
2 months ago
587
10
Laravel

Polymorphic Relationships doesn't fill out the fields

Posted 2 months ago by Foks

Hello, I'm having an issue with polymorphic relationships, when I try to make the relationship it doesn't fill out all the fields that it should.

I'm using the SocilateProviders Discord package.

My Models:

class DiscordAuth extends Model
{
    protected $guarded = [];

    public function auth()
    {
        return $this->morphOne(Auth::class, 'authable');
    }
}
class Auth extends Model
{
    protected $guarded = [];

    public function authable() {
        return $this->morphTo();
    }
}

My migrations:

        Schema::create('auths', function (Blueprint $table) {
            $table->id();
            $table->string('token');
            $table->morphs('authable');
            $table->timestamps();
        });
        Schema::create('discord_auths', function (Blueprint $table) {
            $table->unsignedBigInteger('id')->unique();
            $table->primary('id');
            $table->foreignId('user_id')->constrained('users');
            $table->string('email');
            $table->string('username');
            $table->string('discriminator');
            $table->timestamps();
        });

The controller that creates it

class DiscordController extends Controller
{
    /**
     * Redirect the user to the GitHub authentication page.
     *
     * @return RedirectResponse
     */
    public function redirectToProvider()
    {
        return Socialite::driver('discord')->redirect();
    }

    /**
     * Obtain the user information from GitHub.
     *
     * @return void
     */
    public function handleProviderCallback()
    {
        $user = Socialite::driver('discord')->user();

        $discord = DiscordAuth::create([
            'id' => intval($user->id),
            'user_id' => Auth::user()->id,
            'email' => $user->getEmail(),
            'username' => $user->getName(),
            'discriminator' => $user->user['discriminator']
        ]);

        $discord->auth()->create([
           'token' => $user->refreshToken
        ]);

        dump($discord);
    }
}

Picture of the DB row that doesn't have it's ID filled: https://i.imgur.com/3ucjt88.png

Laravel fills the other fields completely fine, but for some reason, not authable_id

What have I done wrong?

Please sign in or create an account to participate in this conversation.