-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use polymorphic relationship #10
Comments
I think that a better idea would be the ability to change the Personal Access Token model like Passport: https://laravel.com/docs/5.8/passport#overriding-default-models Then you would extend the PersonalAccessToken model and change the It is already in #18 |
@rennokki Correct me if I'm wrong, changing the personal access token model will allow you to issue token to another model than the user one but not several models, right? |
Create a new model <?php
namespace App;
use Laravel\Airlock\PersonalAccessToken;
class MyCustomTokenModel extends PersonalAccessToken
{
public function user()
{
return $this->morphTo();
}
} Make use of the function in use App\MyCustomTokenModel;
use Laravel\Airlock\Airlock;
Airlock::usePersonalAccessTokenModel(
MyCustomTokenModel::class
); Make sure you ignore the migrations in the use Laravel\Airlock\Airlock;
Airlock::ignoreMigrations(); Publish the migrations or copy the migration file from the package in your Schema::create('personal_access_tokens', function (Blueprint $table) {
...
$table->morphs('user'); // let the name 'user' alone because morphTo is greedy due to the user() relationship
...
}); In your model (either it's use Laravel\Airlock\Airlock;
public function tokens()
{
return $this->morphMany(Airlock::$personalAccessTokenModel, 'model');
} Jobs done. |
IMHO it would be simpler that Airlock handle this out of the box using a polymorphic relationship :) Your solution works but it's a bit of extra work for a common use case. |
I took Passport's example on this case. I think stuff should be simple out-of-the-box. :male_shrug: |
I agree to this, I was struggling to customize Laravel Passport to support polymorphic relationship. And I still resolve to use custom HTTP Header hack to get around it. References: |
@taylorotwell Would you accept a PR for this? |
Thank you kind sir! |
Could be useful for issuing tokens for organization accounts.
The text was updated successfully, but these errors were encountered: