naykel
1 month ago
151
2
General

Create redirect method with switch statement

Posted 1 month ago by naykel

Hello, I have created a reusable toolbar setting the buttons with different values so I can perform redirects using a switch statement base on the values. For the most part this works exactly exactly as I want..

public function update(Course $course)   {

    // other validate and save actions goes here

    switch (request('action')) {

        case 'save_stay':
            return back()->with('flash', 'Course saved!');
            break;

        case 'save_close':
            return redirect('admin/courses')->with('flash', 'Course saved!');
            break;

        case 'save_new':
            return redirect(route('admin.courses.create'))->with('flash', 'Course saved!');
            break;

        case 'back':
            return redirect('admin/courses');
            break;
    }
}

This toolbar works for both storeand update methods so I would like to make a resuable function, however the redirects no longer work. What am I missing here?

public function store(Course $course) {

    // other validate and save actions goes here

    $this->redirectTo(request('action'));
}

public function update(Course $course) {

    // other validate and save actions goes here

    $this->redirectTo(request('action'));
}

public function redirectTo($action)  {
    switch ($action) {

        case 'save_stay':
            return back()->with('flash', 'Course saved!');
            break;

        case 'save_close':
            return redirect('admin/courses')->with('flash', 'Course saved!');
            break;

        case 'save_new':
            return redirect(route('admin.courses.create'))->with('flash', 'Course saved!');
            break;

        case 'back':
            return redirect('admin/courses');
            break;
    }
}

Please sign in or create an account to participate in this conversation.