I'm trying to create a blade directive that checks if the User is logged in AND has an activated account. In our database, we have a user_status column with 0: pending, 9 active.
// AppServiceProvider.php
public function boot()
{
Blade::directive('active', function () {
$condition = false;
// check if the user is authenticated
if (Auth::check()) {
// check if the user has a subscription
if(auth()->user()->getStatus() == 9) {
$condition = true;
} else {
$condition = false;
}
}
return "<?php if ($condition) { ?>";
});
Blade::directive('inactive', function () {
return "<?php } else { ?>";
});
Blade::directive('endactive', function () {
return "<?php } ?>";
});
}
// welcome.blade.php
@active
<p>User is active</p>
@inactive
<p>User is inactive</p>
@endactive
I have included this getStatus() function on my User.php model.
public function getStatus() {
return $this->user_status;
}
I can also use the Trait MustActivateAccount, which includes this feature:
/**
* Determine if the user has activated account.
*
* @return bool
*/
public function hasActivatedAccount()
{
return $this->user_status == 9;
}
This blade directive when added to the page says this error:
Facade\Ignition\Exceptions\ViewException
syntax error, unexpected ')'
Am I not escaping correctly in the AppServiceProvider.php file and if so where and how?
auth()->user()->getStatus() == 9useAuth::user()->getStatus() == 9- Gabrielle-M