Answer the question
In order to leave comments, you need to log in
Where is it better to start filtering the passed comment text for later saving?
Task : to implement the ability for some user groups to leave links in the comments, for the rest - to delete links.
Data from the user gets into the controller,
public function store(StoreReviewRequest $request) {}
$validatedData = $request->validated();
abstract class Filter
{
protected $content;
protected $filter;
public function __construct($content,array $filters)
{
$this->content = $content;
$this->filters = $filters;
}
public function apply()
{
// code
}
}
class TextFilter extends Filter
{
/**
* Wrap links filter
*
* @return void
*/
public function wrapLinks()
{
// Code
}
}
if(Gate::allows('useLinkInComments', $request->user()) {
//code
}
new TextFilter($validatedData['message'],['wrap-links'])->apply();
Answer the question
In order to leave comments, you need to log in
There are many ways to do this, but definitely not in the controller.
I would make CommentObserver:
php artisan make:observer CommentObserver --model=Comment
saving(Comment $comment)
I would check whether the user has the right (using a policy, not a gate, by the way) and filter it as it should.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question