Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass array parameter to middleware in laravel

i don't know the syntax of php 7 i'm actually new to it i'm trying to pass roles like ['admin','user','cmc'] to route middleware like shown below how do i do it properly

this is my route

  Route::get('/dashboard','HomeController@dashboard')->name('dashboard')->middleware("roles:['admin','user']");

//how do i pass array of roles in it

//role middleware

<?php

namespace App\Http\Middleware;

use Closure;
use App\Role;
use Illuminate\Support\Facades\Log;


class Roles
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next,$role)
    {
        Log::info($role);
        if($request->user()==null){
            return response('unauthorised Access detected',401);
        }
        //check for any role passed from array 
        if($request->user()->hasAnyRole($role)){
            return $next($request);
        }        
        return response('unauthorised Access detected',401);
    }
}

//usermodel

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function role(){
        return $this->hasMany(Role::class);
    }
    public function hasANyRole($roles){
        if(is_array($roles)){
            foreach($roles as $role){
                if($this->hasRole($role)){
                    return true;
                }
            }return false;
        }else{
            if($this->hasRole($roles)){
                return true;
            }
            return false;
        }
    }
    public function hasRole($role){
        if($this->role()->where('role',$role)->first()){
            return true;
        }
        return false;
    }
    public function getRole(){
    return $this->role()->select('role')->get();
    }

}

how do i pass role like ['admin','user','cmc'] some thing like this into the middleware of role

the problem is i can only get the first value in the array and the rest is not there can some one get me out of this

like image 803
Pavan Kumar Avatar asked Jan 29 '26 00:01

Pavan Kumar


1 Answers

I had a similar situation where I wanted to check if a user is an owner or admin before visiting a route; so I didn't need duplicate routes, and needed to pass an array instead of set single parameters.

I went down the route of sending a single paramter but using an | as a delimiter to explode on the middlewhere class.

In the route I had this for the route group Route::group(['middleware' => ['checkRole:admin|owner']], function () {

and then in the middlewhere I used explode

$roles = explode('|', $permitted_roles);

Simple looped through the roles array to check if the user had one of the roles :) Hope this helps. Simple and easy for what I needed.

like image 60
Sjc311 Avatar answered Jan 30 '26 15:01

Sjc311



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!