Answer the question
In order to leave comments, you need to log in
How to extend a project on Laravel?
Good afternoon!
I ask you to look at the following code and criticize :)
We have both authorized and unauthorized users in the application.
For the most part, we'll be working with the contents table and a bit of user subscriptions.
Content can be either absolutely free (available) or paid. To be more precise, in order to get it, the user must have a subscription, and of course, when he requests it, he must be authorized.
I'm using React on the frontend, and came up with locked .
locked - is responsible for whether we will display to the user that this content is available or not. We display it simply - in the form of a castle.
Content Model
protected $hidden = ['secret'];
protected $appends = ['locked'];
public function getLockedAttribute()
{
// Платный ли контент.
if ($this->attributes['is_paid'] == true) {
// Проверяем авторизован ли пользователь или на наличие подписки
if (! Auth::check() || Carbon::parse( Auth::user()->membership_ends_at ) < Carbon::now() {
return $this->attributes['locked'] = true;
}
}
// По умолчанию контент доступный.
return $this->attributes['locked'] = false;
}
foreach ($rootModel->contents as $content) {
if (! $content->locked) {
// Делаем видимым наш secret.
$content->makeVisible('secret');
}
}
Answer the question
In order to leave comments, you need to log in
Create a policy , attach it to the resource controller, write in the view method of the policy
return !$post->isPaid or optional($user)->hasSubscription;
Gate::allows('view', $this)
return $this->membership_ends_at >= now();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question