38 lines
844 B
PHP
38 lines
844 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models\Traits;
|
|
|
|
use App\Models\User;
|
|
use App\Services\UserScopeApplier;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
trait UserScopedModel
|
|
{
|
|
public function getUserColumnId(): string
|
|
{
|
|
return 'resume.user_id';
|
|
}
|
|
|
|
protected static function bootUserScopedModel(): void
|
|
{
|
|
$scopeApplier = app(UserScopeApplier::class);
|
|
|
|
$scopeApplier->applyScope(new static());
|
|
|
|
if ((new static())->getUserColumnId() === 'user_id') {
|
|
|
|
static::creating(function (Model $model) {
|
|
/** @var User $user */
|
|
$user = Auth::user();
|
|
Assert::notNull($user);
|
|
|
|
$model->fill(['user_id' => $user->getKey()]);
|
|
});
|
|
}
|
|
}
|
|
}
|