52 lines
1.3 KiB
PHP
52 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Concerns\HasUuid;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Customer extends Model
|
|
{
|
|
/** @use HasFactory<\Database\Factories\CustomerFactory> */
|
|
use HasFactory, HasUuid;
|
|
|
|
protected $fillable = ['user_id', 'name', 'email', 'locale', 'stripe_customer_id', 'status'];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function orders(): HasMany
|
|
{
|
|
return $this->hasMany(Order::class);
|
|
}
|
|
|
|
public function instances(): HasMany
|
|
{
|
|
return $this->hasMany(Instance::class);
|
|
}
|
|
|
|
/** Find or create the portal login account for this customer and link it. */
|
|
public function ensureUser(): User
|
|
{
|
|
if ($this->user) {
|
|
return $this->user;
|
|
}
|
|
|
|
$user = User::query()->firstOrCreate(
|
|
['email' => $this->email],
|
|
['name' => $this->name, 'password' => Hash::make(Str::random(40)), 'is_admin' => false],
|
|
);
|
|
|
|
$this->update(['user_id' => $user->id]);
|
|
|
|
return $user->refresh();
|
|
}
|
|
}
|