auto create an organisation for a user when they sign up
This commit is contained in:
@@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Organisation extends Model
|
||||
{
|
||||
@@ -29,4 +30,14 @@ class Organisation extends Model
|
||||
{
|
||||
return $this->hasMany(Application::class);
|
||||
}
|
||||
|
||||
public static function createUniqueSlug(string $name): string
|
||||
{
|
||||
$slug = Str::slug($name);
|
||||
$count = 2;
|
||||
while (Organisation::where('slug', $slug)->exists()) {
|
||||
$slug = Str::slug($name) . '-' . $count++;
|
||||
}
|
||||
return $slug;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
|
||||
use App\Enums\OrganisationRole;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
@@ -12,6 +14,20 @@ class User extends Authenticatable
|
||||
/** @use HasFactory<\Database\Factories\UserFactory> */
|
||||
use HasFactory, Notifiable;
|
||||
|
||||
public static function boot()
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::created(function (User $user) {
|
||||
$organisation = Organisation::create([
|
||||
'name' => $user->name,
|
||||
'slug' => Organisation::createUniqueSlug($user->name),
|
||||
'owner_id' => $user->id,
|
||||
]);
|
||||
$organisation->members()->attach($user, ['role' => OrganisationRole::Admin]);
|
||||
});
|
||||
}
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
|
||||
Reference in New Issue
Block a user