Search code examples
phplaravel-5middlewarecontrollers

How can I pass 2 parameters to my middle-ware on my admin controller


I have an admin-controller and i want to passe to it middle-ware to parameters to allow Super Admin to do admin stuff.

Here my controller:

class adminController extends Controller
{
    public function __construct(){

        $this->middleware('admin');
    }
    public function showGenerateCode(){
        $code ="";
        return view('create_code',compact('code'));
    }
    public function generateCode(Request $request){
        $phone = $request['phone'];
        $code = "";
        $user = User::where('phone' ,'=',$phone)->first();
        if($user){
            $code = "Ce numero de telephone et deja associé a un utilisateur";
            return view('create_code',compact('code'));
            //dd($code);
        }
        else{
            $code = "Code d'autorisation générer avec succé : ".substr($phone,1,3).rand(1000000,9999999);
            return view('create_code',compact('code'));
        }
    }
}

Solution

  • Pass them as an array

    $this->middleware(['admin', 'superAdmin']);
    

    If it does not work with an array, pass each of them separately

    $this->middleware('admin');
    $this->middleware('superAdmin');
    

    Not a solution to your issue, but a workaround since everything you try is not working. You could remove the middleware from the constructor and instead apply them to the routes that call the functions from that constructor

    Route::group(['middleware' => ['admin', 'superadmin']], function(){
        Route::get('url1', 'adminController@function1');
        Route::get('url2', 'adminController@function2');
        Route::post('url3', 'adminController@function3');
    });
    

    Try that and see how it goes.