英文:
Passing data to Laravel view based on user ID gives undefined variable error, how to fix?
问题
这是您的代码部分的翻译:
namespace App\Http\Controllers;
use App\Models\User;
use App\Models\bedDetails;
use Illuminate\Support\Facades\Auth;
class HomeController extends Controller
{
// 根据用户类型重定向用户
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');
}
}
}
<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 $viewSurgicalBedDetail)
<tr>
<td>{{ $viewSurgicalBedDetail->fullComplement }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
以上是代码部分的翻译。
英文:
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 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>
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.
答案1
得分: 1
你遇到的问题是,当另一个用户以不同的用户类型登录时,在该用户类型的控制器方法中未定义变量$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'));
}
}
希望这对你有帮助。
英文:
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'));
}
}
答案2
得分: 0
你的问题可能是你在 foreach
循环中使用了两个相同的变量名:
@foreach ($viewSurgicalBedDetails as $viewSurgicalBedDetail )
将你的 foreach 修改成像这样:
@foreach ($viewSurgicalBedDetails as $viewDetail )
<tr>
<td>{{ $viewDetail->fullComplement }}</td>
</tr>
@endforeach
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论