Have you ever heard about validation?
Redirect from called function based on condition
Hi the following is my code. Basically the condition is quite complicate in my real project but the following should give you an idea of what I want to achieve.
public function store(StorePostRequest $request)
{
$channel = Channel::with(['courseCategory'])->where('id', $request->get('channel_id'))->first();
$this->createCourseCheck($channel);
Post::create([
//my fields
]);
}
private function createCourseCheck($channel)
{
if ($this->userRole() != 'admin' && !empty($channel->course_category_id)) {
if ($channel->courseCategory['slug'] == 'my-courses') {
$courseRegistration = CourseRegistration::query()
->join('my_courses', 'my_courses.parent_id', '=', 'course_registrations.channel_id')
->where([
'my_courses.channel_id' => $channel->id,
'course_registrations.user_id' => $this->user()->id
])
->count();
/** Is registered to course and can create post */
if ($courseRegistration > 0) {
return true;
}
}
session()->flash('error', 'You dont have the privilages access');
return redirect()->route('posts.create')->send();
}
}
While storing the POST check for a categoryExists if not exists I want to redirect from that called function.
I know I can achieve the same by putting the code in store method but the problem is I am using this kind of condition like 8 to 10 times in my code.
I don't want to duplicate the code
It doesn't matter if you have one or one hundred roles.
The Gate logic will perform some truth test for the current authenticated user (based on whatever criteria you decide); this will either authorize the user to take the action, or not.
Please or to participate in this conversation.