0

I'm fairly new to the Laravel. I implemented AUTH in laravel with 3 roles. I've HomeController with index function where I'm checking for what is the role of the logged in user and depending upon the role I want to load the view. I'm not able to get how do I change the route name from /home to /superadmin. When I load view it only shows me text but the route is still /home. I know it must have been answered somewhere but nothing is working for me right now.

I've tried this

  Redirect::to('superadmin.page1')->with($data);
            return view('superadmin.page1')->with($data);
            return redirect()->route('superadmin');
            return view('superadmin.page1');

HomeController.php //main code

try
        {
            if(count($data) < 0)
            {
                return view('user.page1')->with($data);
            }
            else
            {
                //super admin ko pehle check karo , then admin , then user
                $role_array = array();
                foreach ($data as $key => $value) {
                        $role = $value->role_name;
                        array_push($role_array , $role);
                }

           if (in_array("ROLE_SUPERADMIN", $role_array))
           {
            return view('superadmin.page1')->with($data);
           }
           else if (in_array("ROLE_ADMIN", $role_array))
           {
            return view('admin.page1')->with($data);
           }
           else
           {
            // undefined role found, therefore user 
            return view('user.page1')->with($data);
           }
        }
    }

==========

My route

Auth::routes();

Route::get('/home', 'HomeController@index');

Route::get('/superadmin', 'SuperAdminController@index');

Route::get('/superadminPage1', 'SuperAdminController@page1');

I'm getting this as output

@extends('layouts.app') @section('content')
SuperAdmin Page1
@if (session('status'))
{{ session('status') }}
@endif This is SuperAdmin PAGE1. You must be privileged to be here !
@endsection
  • You just have shown HomeController code but you have two controllers in routes HomeController and SuperAdminController – lkdhruw Sep 27 '19 at 07:10
  • Oh i got the solution , i need to redirect with return redirect()->action('SuperAdminController@index')->with($data); – bilal malik Sep 27 '19 at 07:20
  • @ikdhruw Now how do I get data from from Homecontroller>SuperAdminController to its view OR from superAdminController to its view, i'm getting undefined $data in view – bilal malik Sep 27 '19 at 07:29
  • If you are returning view from the controller just check whether you passing data correctly or not. If you want to use the redirect method use then use named route it makes things much simpler. your SuperAdminController confused me initially – lkdhruw Sep 27 '19 at 07:31
  • I'm passing data like this from SuperAdminController - return view('superadmin.home')->with($data); but in view side I'm getting undefined $data – bilal malik Sep 27 '19 at 07:33
  • Check this out https://stackoverflow.com/a/18341826/2669814 and https://stackoverflow.com/a/25078560/2669814. These answers explained how to pass the data – lkdhruw Sep 27 '19 at 07:35
  • On doing this - return view('superadmin.home')->with(["data"=>$data]); I'm getting htmlspecialchars() expects parameter 1 to be string, array given (View: – bilal malik Sep 27 '19 at 07:40
  • My data is an array which has objects inside it – bilal malik Sep 27 '19 at 07:40
  • It is `->with("data"=>$data)` not `->with(["data"=>$data])` – lkdhruw Sep 27 '19 at 07:45
  • syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' – bilal malik Sep 27 '19 at 07:48
  • I also missed this one this should work `->with('data', $data)`. ref. https://stackoverflow.com/a/35326267/2669814 – lkdhruw Sep 27 '19 at 07:51
  • Yes, now working. thanks alot man. – bilal malik Sep 27 '19 at 09:47

0 Answers0