Storing Cashier information in separate, related model: Users > UserPaymentMethod
I know that Cashier lets you set up which models are used by the package, but I don't seem to fully understand if what I am trying to do is possible.
Really, all I am trying to do is store the Stripe Customer information in a separate table, while also still relating it to the User model, for instance:
$user = User::find($id);
$paymentMethod = $user->defaultPaymentMethod;
where the defaultPaymentMethod is defined as:
public function defaultPaymentMethod()
{
$this->hasOne(UserPaymentMethod::class)->where('default', true);
}
So, any given $user is a stripe customer, but they have multiple payment methods saved in the table of the UserPaymentMethod model. Because Stripe can return a payment method ID for each method saved for the customer, I would store the stripe_id and the stripe_payment_method_id in the UserPaymentMethod.
I still want to be able to do $user->createAsStripeCustomer($options); and have that functionality, but save the results on the UserPaymentMethod model, not in the User model / table.
My UserPaymentMethod:
class UserPaymentMethod extends Model
{
use Billable;
protected $table = 'user_pm';
protected $guarded = [];
protected $casts = [
'default' => 'boolean',
];
public function user()
{
return $this->belongsTo(User::class);
}
}
my User model:
class User extends Authenticatable implements \Spatie\Onboard\Concerns\Onboardable
{
use HasApiTokens,
HasFactory,
Notifiable,
HasRoles,
protected $fillable = [
'name',
'email',
'password',
'uuid',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function defaultPaymentMethod()
{
$this->hasOne(UserPaymentMethod::class)->where('default', true);
}
public function paymentMethods()
{
return $this->hasMany(UserPaymentMethod::class);
}
}
Any thoughts?
Please or to participate in this conversation.