Certificate manager built on Laravel 13, Livewire 4, and Flux. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
27 lines
591 B
PHP
27 lines
591 B
PHP
<?php
|
|
|
|
namespace App\Models\Concerns;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
trait OwnedByUser
|
|
{
|
|
protected static function bootOwnedByUser(): void
|
|
{
|
|
static::creating(function (self $model) {
|
|
if (! isset($model->user_id) && ($user = Auth::user()) !== null) {
|
|
$model->user_id = $user->id;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
}
|