根据用户ID向Laravel视图传递数据会出现未定义变量错误,如何修复?

olmpazwi  于 2023-05-30  发布在  其他
关注(0)|答案(2)|浏览(101)

你好,请我是laravel的新手,我试图根据登录的用户类型的用户ID将数据传递到laravel中的视图。它为第一个用户工作,但当另一个用户登录时,它给我一个未定义的变量错误。

namespace App\Http\Controllers;

use App\Models\User;
use App\Models\bedDetails;
use Illuminate\Support\Facades\Auth;

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>

上面的代码显示了存储在数据库表中的数据。
我检查了在compact上传递的名称是否与foreach循环中使用的名称相同。

hrysbysz

hrysbysz1#

您面临的问题是,当另一个用户以不同的用户类型登录时,变量$viewSurgicalBedDetails没有在该用户类型的控制器方法中定义。因此,当试图在视图中访问它时,您会得到一个“未定义变量”错误。
下面是代码的更新版本:

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'));
    }
}
cyvaqqii

cyvaqqii2#

你的问题可能是你在foreach循环中使用了两个相同的变量名:

@foreach ($viewSurgicalBedDetails as $viewSurgicalBedDetails )

把你的foreach改成这样:

@foreach ($viewSurgicalBedDetails as $viewDetail )
    <tr>
        <td>{{ $viewDetail->fullComplement }}</td>
    </tr>
@endforeach

相关问题