dimanche 3 mars 2019

Laravel - redirect from controller method not working

I have this method in my DocumentsController and am trying to implement some simple permissions system, in that a user that is not an admin must have been assigned a branch and a department before adding, editing or deleting a Document. Here is the code for the method

    /**
     * Check the credentials of the user that is not an admin
     * to add, modify a document
     */
    private function checkCredentials() {
        $user = auth()->user();
        // dd((!is_admin() && !$user->branch_id && !$user->department_id));
        if (!is_admin() && !$user->branch_id && !$user->department_id) {
            // dd(redirect()->route('documents'));
            return redirect()->route('documents')->with([
                'class' => 'alert-danger',
                'message' => 'Ask your administrator to assign you a branch and department first',
            ]);
        }
    }

And here is how am calling it in one of my controller methods that mapped to the route Route::get('/documents/add', ['as' => 'documents.add', 'uses' => 'DocumentsController@create',]);

public function create()
{
    $this->checkCredentials();

    ...

    return view('main/documents/create', $data);
}

Problem is the redirection is not working as it continues to display the form, even when the user has not yet been assigned a branch or department, that is when both the branch_id and department_id are both equal to null.

What could be the reason for this? Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire