skip to Main Content

Hello Please I am new to laravel I am trying to pass data to the view in laravel based on the userid of the usertype logged in. It worked for the first user but it is giving me an undefined variable error when another user logs in.

namespace AppHttpControllers;

use AppModelsUser;
use AppModelsbedDetails;
use IlluminateSupportFacadesAuth;

class HomeController extends Controller
{
    //redirect user based on the usertype
    public function redirect()
    {
        $usertype = Auth::user()->usertype;

       
       
        if ($usertype == 'headNurseMedical') {
            $viewBedDetails = bedDetails::all()->where('userID', '=', '3');
            return view('HeadNurseMedicine.dashboard', compact('viewBedDetails'));


            
        } elseif ($usertype == 'headNurseNASurgery') {
            $viewSurgicalBedDetails = bedDetails::all(); 
            return view('HeadNurseNASurgery.dashboard', compact('viewSurgicalBedDetails'));
}else{

            return view('administrator.dashboard');
        }

This is the code that performs the redirect based on the usertype 
<div class="card-body">
                          <div class="table-responsive">
                             <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                                    <thead>
                                        <tr>
                                            <th>SW 1</th>
                                            <th>SW 2</th>
                                            <th>SW 3</th>
                                            <th>SW 4</th>
                                            <th>SW 5</th>
                                            <th>SW 6</th>
                                            <th>WARD G</th>
                                            <th>TROLLEYS</th>
                                            <th>PAEDIATRIC SURGERY WARD 1</th>
                                        </tr>
                                    </thead>
                                    
                                   
                                    <tbody>
                                       
                                        @foreach ($viewSurgicalBedDetails as $viewSurgicalBedDetails )
                                            <tr>
                                                <td>{{ $viewSurgicalBedDetails->fullComplement }}</td>
                                            </tr>
                                        @endforeach
                                       
                                      
                                    </tbody>
                                </table>
                          </div>
                      </div>
                  </div>

The above code shows the data stored in the database table.

I checked to see if the name passed on the compact is the same as the name going to be used in the foreach loop.

2

Answers


  1. Your issue could be that you are using two of the same variable names in your foreach loop:

    @foreach ($viewSurgicalBedDetails as $viewSurgicalBedDetails )
    

    Change your foreach to something like this:

    @foreach ($viewSurgicalBedDetails as $viewDetail )
        <tr>
            <td>{{ $viewDetail->fullComplement }}</td>
        </tr>
    @endforeach
    
    Login or Signup to reply.
  2. The issue you’re facing is that when another user logs in with a different user type, the variable $viewSurgicalBedDetails is not defined in the controller method for that user type. Hence, you’re getting an "undefined variable" error when trying to access it in the view.

    Here’s an updated version of your code:

    public function redirect()
    {
        $usertype = Auth::user()->usertype;
    
        
        $viewSurgicalBedDetails = [];
    
        if ($usertype == 'headNurseMedical') {
            $viewBedDetails = bedDetails::where('userID', '=', '3')->get();
            return view('HeadNurseMedicine.dashboard', compact('viewBedDetails'));
        } elseif ($usertype == 'headNurseNASurgery') {
            $viewSurgicalBedDetails = bedDetails::all();
            return view('HeadNurseNASurgery.dashboard', compact('viewSurgicalBedDetails'));
        } else {
            return view('administrator.dashboard', compact('viewSurgicalBedDetails'));
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search