Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

channaveer's avatar

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

0 likes
10 replies
channaveer's avatar

Hi, @michaloravec I know about the Request validation and everything. I have updated my code. Please check now.

channaveer's avatar

To be honest it's really difficult for me to explain the whole process. But if you could help me how to redirect form called function it will be really helpful.

tykus's avatar

how to redirect form called function

You cannot.

Move the createCourseCheck logic into the authorize method of the StorePostRequest class; and in the failedAuthorization method of the same class, do the redirect.

channaveer's avatar

@tykus thank you very much for the help. But this code I need to reuse in many places and as I dont want to repeat the code I have to find some alternative.

For the time being, I have done like the following

if (!$this->createCourseCheck($channel)) {
    session()->flash('error', 'You dont have the privilages access');
    return redirect()->action('PostController@create');
}

But if there are any alternatives I wanted to check. I didnt like the idea to put lot of if conditions wherever I use so I want something to be redirect from the called function.

channaveer's avatar

In my application, there are only 3 roles which is Trainer, Admin & Student. Using Gates wouldnt it complicate the system?

Please shed me some light. I am kind of newbee to Laravel

tykus's avatar
tykus
Best Answer
Level 104

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.

1 like
channaveer's avatar

Cool thank you very much. I Will start using the Gates.

Please or to participate in this conversation.