根据用户ID将数据传递到Laravel视图会导致未定义变量错误,如何修复?

huangapple go评论63阅读模式
英文:

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()-&gt;usertype;

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


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

            return view(&#39;administrator.dashboard&#39;);
        }

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

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()-&gt;usertype;

    
    $viewSurgicalBedDetails = [];

    if ($usertype == &#39;headNurseMedical&#39;) {
        $viewBedDetails = bedDetails::where(&#39;userID&#39;, &#39;=&#39;, &#39;3&#39;)-&gt;get();
        return view(&#39;HeadNurseMedicine.dashboard&#39;, compact(&#39;viewBedDetails&#39;));
    } elseif ($usertype == &#39;headNurseNASurgery&#39;) {
        $viewSurgicalBedDetails = bedDetails::all();
        return view(&#39;HeadNurseNASurgery.dashboard&#39;, compact(&#39;viewSurgicalBedDetails&#39;));
    } else {
        return view(&#39;administrator.dashboard&#39;, compact(&#39;viewSurgicalBedDetails&#39;));
    }
}

答案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 )
    &lt;tr&gt;
        &lt;td&gt;{{ $viewDetail-&gt;fullComplement }}&lt;/td&gt;
    &lt;/tr&gt;
@endforeach

huangapple
  • 本文由 发表于 2023年5月24日 20:26:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定