Category : middleware

I am facing an issue in laravel ,where I have role_permission_table: here is the structure of that : migration file: Schema::create(‘role_permissions’, function (Blueprint $table) { $table->id(); $table->unsignedBigInteger(‘role_id’); $table->unsignedBigInteger(‘Item_id’); $table->unsignedBigInteger(‘permission_id’); $table->foreign(‘role_id’)->references(‘id’)->on(‘roles’); $table->foreign(‘Item_id’)->references(‘id’)->on(‘items’); $table->foreign(‘permission_id’)->references(‘id’)->on(‘permissions’); $table->timestamps(); }); And here is the DB table of that in PHPMyAdmin: enter image description here Now I have a role based on ..

Read more

Laravel is ignoring my middleware. I have a middleware that checks that you have billing information on our website. If you have them but they are not validated by the administrator, the middleware must continue blocking. Or, if they are rejected. class HasBilling { public function handle($request, Closure $next) { $user_profile = UsersProfile::where(‘user_id’, Auth::id())->first(); if ..

Read more

using AuthCheck middleware and just trying to check Has Username in session but it gives an error Error Description is below Error ErrorException Attempt to read property "headers" on string LoginController C:xampphtdocslarave8authvendorlaravelframeworksrcIlluminateFoundationHttpMiddlewareVerifyCsrfToken.php:191 <?php namespace AppHttpControllers; use IlluminateHttpRequest; use IlluminateSupportFacadesHash; use AppModelsUser; use IlluminateSupportFacadesSession; class LoginController extends Controller { function login() { return view(‘login.login’); } function ..

Read more

I am facing an issue where I have assigned the permission like add , edit , view , delete to items like post and comments which are further assigned to particular roles like admin, user , editor . lets take an example: A admin(role) can perform permissions say add,edit,delete(permission) on particular item say he can ..

Read more

I’m guarding a route via a middleware check for a valid token, like thus;- http://localhost:8097/init/my-secret-token Route::get(‘/init/{token}’, [MyController::class, ‘init’])->middleware(‘validToken’); // Kernel.php protected $routeMiddleware = [ ‘validToken’ => AppHttpMiddlewareEnsureTokenIsValid::class, ]; // App/Http/Middleware/EnsureTokenIsValid.php class EnsureTokenIsValid { public function handle(Request $request, Closure $next) { dd($request->input(‘token’)); // returns null if($request->input(‘token’) !== ‘my-secret-token’){ return redirect(‘home’); } return $next($request); } } $request->input(‘token’) ..

Read more

I want to manage the authorization in the routes so I create a middleware called "role" This is the role middleware handle function: public function handle(Request $request, Closure $next) { $role = Auth::user()->role_id; $emp_id = Auth::user()->employee_id; $clt_id = Auth::user()->client_id; if ($role == 3745639763) { return $next($request); } else if ($role == 1 && $emp_id == ..

Read more

I have a route group which uses the guest middleware Route::group([‘middleware’ => ‘guest’],function(){ Route::resource(‘posts’,’PostController’)->only([‘show’]); }); The problem i am facing is this show route from the Post resource can be viewed only if the post has been approved or the owner is the Authorized user. But the problem is when i access the Auth::id() in ..

Read more

Can somebody explain why this strange behavior of Laravel is happening? Basically, I am trying to create a middleware for my application public function handle(Request $request, Closure $next) { if (auth()->check()) { $expires = Carbon::now()->addMinute(2); IlluminateSupportFacadesCache::put(‘user-is-online-‘ . Auth::user()->id, true, $expires); } return $next($request); } } But auth()->check it is keep failing and not returning true ..

Read more

I have a simple question in laravel8 I tested two codes, but I didn’t find any differnces. they look like same for me. even I clicked tons of time and both gave me ‘too many requests’. Route::middleware(‘api’)->get(‘/user’, function (Request $request) { return "aaa"; }); Route::get(‘/user’, function (Request $request) { return "aaa"; }); what is the ..

Read more

This my Web Route Route::group( [ ‘prefix’ => LaravelLocalization::setLocale(), ‘middleware’ => [ ‘localeSessionRedirect’, ‘localizationRedirect’, ‘localeViewPath’] ], function(){ Route::group([‘prefix’ => ‘admin’], function () { Route::get(‘/’, ‘[email protected]’)->name(‘admin.index’); Route::get(‘/adminlogin’, ‘[email protected]’)->name(‘adminlogin’); Route::post(‘/adminPostLogin’, ‘[email protected]’)->name(‘adminPostLogin’); When I try to do something in Liveware this give like this error!!! Failed to load resource: the server responded with a status of 404 (Not ..

Read more

This my Web Route Route::group( [ ‘prefix’ => LaravelLocalization::setLocale(), ‘middleware’ => [ ‘localeSessionRedirect’, ‘localizationRedirect’, ‘localeViewPath’] ], function(){ Route::group([‘prefix’ => ‘admin’], function () { Route::get(‘/’, ‘[email protected]’)->name(‘admin.index’); Route::get(‘/adminlogin’, ‘[email protected]’)->name(‘adminlogin’); Route::post(‘/adminPostLogin’, ‘[email protected]’)->name(‘adminPostLogin’); When I try to do something in Liveware this give like this error!!! Failed to load resource: the server responded with a status of 404 (Not ..

Read more

I’m using Laravel 8 for my project and in this project and I have created a custom Middleware called Admin that goes like this: public function handle(Request $request, Closure $next) { if(Auth::check()){ if(Auth::user()->isAdmin()){ return $next($request); } } return redirect(‘/’); } And I tried applying it like this: Route::group([‘middleware’ => ‘admin’], function(){ Route::resource(‘admin/users’,AppHttpControllersAdminAdminUserController::class); }); And on ..

Read more

So I made a little chat functionality in laravel using pusher so it updates in real time. I have an event with the following methods I call using broadcast(new MessageSent($message, $conversationId)); whenever a message is sent. public function broadcastOn() { return [‘chat-‘ . $this->conversationId]; } public function broadcastAs() { return ‘new-message’; } And a piece ..

Read more

AppHttpControllersAdminExampleController.php public function index() { dd(‘index’); } appPoliciesExamplePolicy.php public function viewAny(User $user) { return true; } appProvidersAuthServiceProvider.php protected $policies = [ ‘AppModelsExample’ => ‘AppPoliciesExamplePolicy’, ]; routes/admin.php Route::get(‘example’, [ExampleController::class, ‘index’])->middleware(‘can:viewAny,AppModelsExample’) But the ->middleware(‘can:viewAny,AppModelsExample’) always returns 403, and when I remove it, the dd(‘index’) will run. Have I missed something? Sourc..

Read more