Laravel 给了我 Facade\Ignition\Exceptions\ViewException 未定义变量:GENERAL_SETTING

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/58424191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:22:48  来源:igfitidea点击:

Laravel is giving me Facade\Ignition\Exceptions\ViewException Undefined variable: GENERAL_SETTING

phphtmlcsslaravel

提问by Rismada Gerra Nindya

This is my controller code to show data from GENERAL_SETTING table

这是我的控制器代码,用于显示 GENERAL_SETTING 表中的数据

    public function show()
    {
        // mengambil data dari table GENERAL_SETTING
        $GENERAL_SETTING = GeneralSettingModel::all();

        // mengirim data GENERAL_SETTING ke view /home
        return view('/home',['GENERAL_SETTING' => $GENERAL_SETTING]);
    }

This is the code in my view (home.blade.php)

这是我认为的代码(home.blade.php)

<div class="table-responsive">
    <table class="table table-bordered">
        @foreach($GENERAL_SETTING as $gs)
        <thead class="bg-primary">
            <tr class="border-1">
                <th scope="col" colspan="2" class="border-1">Hardware</th>
                <th scope="col" colspan="2" class="border-1">Communication</th>
                <th scope="col" class="border-1">Master Clock</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td colspan="2" >IP: {{ $gs->IP_HW }}</td>
                <td colspan="2" >SNMP Driver
                    <br>
                        Status: Connected
                    </br>
                </td>
                <td rowspan="4">IP: IP: {{ $gs->IP_MC }}
                    <br>
                        Status: Connecteds
                    </br>
                </td>
            </tr>
            <tr>
                <td colspan="2" >Temperature: 31</td>
                <td colspan="2" >Cloud Driver
                    <br>
                        URL: <a href="cloud.mqtt.com" target="_blank"> cloud.mqtt.com </a>
                    </br>
                    <br>
                        Status: Connected
                    </br>
                </td>
            </tr>
            <tr>
                <td class="bg-primary">Main</td>
                <td class="bg-primary">Backup</td>
                <td class="bg-primary">Master Driver</td>
                <td class="bg-primary">Slave Driver</td>
            </tr>
            <tr>
                <td>IP: {{ $gs->IP_SERVER1 }}
                    <br>
                        Status: Connected
                    </br>
                </td>
                <td>IP: {{ $gs->IP_SERVER2 }}
                    <br>
                        Status: Connected
                    </br>
                </td>
                <td>IP: 192.168.1.15
                    <br>
                        Status: Connected
                    </br>
                </td>
                <td>IP: 192.168.1.16
                    <br>
                        Status: Connected
                    </br>
                </td>
            </tr>
        </tbody>
        @endforeach
    </table>
</div>

And this is my routes (web.php)

这是我的路线(web.php)

Auth::routes();
Route::get('/home', 'FEPController@home');
Route::get('/home','GeneralSettingController@show');
Route::get('/home', 'HomeController@index')->name('home');

I try that code in other file (ex: dashboard.blade.php), that code can run normally. But, if I using home.blade.php, that error always show. What should I do to fix that error? Thank u.

我在其他文件中尝试该代码(例如:dashboard.blade.php),该代码可以正常运行。但是,如果我使用 home.blade.php,则始终会显示该错误。我该怎么做才能修复该错误?感谢你。

回答by Jesus Erwin Suarez

In your view()just remove the forward slash in /homeand it should look like this

在你view()刚刚删除正斜杠/home,它应该是这样的

 return view('home',['GENERAL_SETTING' => $GENERAL_SETTING]);.

Just a piece of advice, your code is very poor and you should do it like this

只是一个建议,你的代码很差,你应该这样做

Controller

控制器

public function show()
{
    // mengambil data dari table GENERAL_SETTING
    $generalSettings  = GeneralSetting::all();

    // mengirim data GENERAL_SETTING ke view /home
    return view('/home',['generalSettings' => $generalSettings]);
}

Blade

刀刃

 @foreach($generalSettings as $gs)
   //
 @endforeach

More details

更多细节

https://laravel.com/docs/5.7/views

https://laravel.com/docs/5.7/views

https://www.php-fig.org/psr/psr-2/

https://www.php-fig.org/psr/psr-2/

Hope that helps.

希望有帮助。