为什么我在Laravel上得到未定义的变量

时间:2018-07-14 07:29:06

标签: php laravel

我遇到错误

  

未定义变量:map_tasik

我想在控制器上添加两个方法,并且可以在一个视图上调用该方法

在如下所示路线上编写Laravel代码

Route::middleware('auth')->group(function() {
    Route::get('rsk_down_maps', 'rskMaps@rsk_down_maps_tasik');
    Route::get('rsk_down_maps', 'rskMaps@rsk_down_maps_banjar');
});

控制器上的代码Laravel

public function rsk_down_maps_tasik() {
        $rsk_tasik = DB::select("SELECT * FROM gmaps_geocache WHERE address LIKE '%RSK%' AND address LIKE '%tasikmalaya%'");

                $config['center'] = 'Telkom Tasikmalaya, Kota Tasikmalaya';
                $config['zoom'] = '16';
                $config['map_height'] = '500px';
                $config['geocodeCaching'] = true;
                $config['scrollwheel'] = false;                        
                GMaps::initialize($config);

                //add marker
                $marker['position'] = 'RSK Jl. Ir. H. Juanda No. 906 Tasikmalaya 906, Kota Tasikmalaya';
                $marker['infowindow_content'] = 'KFTD Tasikmalaya WIFI ID';
                $marker['icon'] = 'https://chart.googleapis.com/chart?chst=d_map_spin&chld=0.4|0|FF0000|9|b|AP';
                GMaps::add_marker($marker);

                $map_tasik = GMaps::create_map();

                return view('data.data_rsk_ap.rsk_down_maps', 
                    compact('map_tasik',
                            'rsk_tasik'));
    }

    public function rsk_down_maps_banjar() {
        $rsk_banjar = DB::select("SELECT * FROM gmaps_geocache WHERE address LIKE '%RSK%' AND address LIKE '%banjar%'");

                $config['center'] = 'Plaza Telkom Banjar, Kota Banjar';
                $config['zoom'] = '16';
                $config['map_height'] = '500px';
                $config['geocodeCaching'] = true;
                $config['scrollwheel'] = false;                        
                GMaps::initialize($config);

                // add marker
                $marker['position'] = 'RSK Kantor Dinas Pendidikan Banjar;Jl. Gerilya Pamokolan, Kota Banjar';
                $marker['infowindow_content'] = 'Kantor Dinas Pendidikan Banjar';
                $marker['icon'] = 'https://chart.googleapis.com/chart?chst=d_map_spin&chld=0.4|0|FF0000|9|b|AP';
                GMaps::add_marker($marker);

                $map_banjar = GMaps::create_map();

                return view('data.data_rsk_ap.rsk_down_maps', 
                    compact('map_banjar',
                            'rsk_banjar'));
    }

在View上这样的代码

<div class="active tab-pane" id="tasikmalaya">

                    <div class="box-body">
                        {!! $map_tasik['js'] !!}
                        {!! $map_tasik['html'] !!}
                    </div>
</div>

<div class="tab-pane" id="banjar">
                            <div class="box-body">
                                {!! $map_banjar['js'] !!}
                                {!! $map_banjar['html'] !!}
                            </div>
                </div>

为什么会出现错误的未定义变量? 是因为我在一个控制器中使用了太多的路由?

1 个答案:

答案 0 :(得分:0)

因为视图只能在控制器的一个方法中使用变量,如果要在同一视图中使用两个变量,则应将它们全部传递给视图,您的控制器可能会这样:

public function rsk_down_maps_tasik() {
    $rsk_tasik = DB::select("SELECT * FROM gmaps_geocache WHERE address LIKE '%RSK%' AND address LIKE '%tasikmalaya%'");

            $config['center'] = 'Telkom Tasikmalaya, Kota Tasikmalaya';
            $config['zoom'] = '16';
            $config['map_height'] = '500px';
            $config['geocodeCaching'] = true;
            $config['scrollwheel'] = false;                        
            GMaps::initialize($config);

            //add marker
            $marker['position'] = 'RSK Jl. Ir. H. Juanda No. 906 Tasikmalaya 906, Kota Tasikmalaya';
            $marker['infowindow_content'] = 'KFTD Tasikmalaya WIFI ID';
            $marker['icon'] = 'https://chart.googleapis.com/chart?chst=d_map_spin&chld=0.4|0|FF0000|9|b|AP';
            GMaps::add_marker($marker);

            $map_banjar = GMaps::create_map();
            $rsk_tasik = GMaps::create_map();

            return view('data.data_rsk_ap.rsk_down_maps', 
                compact('map_tasik',
                        'rsk_tasik'));
}

public function rsk_down_maps_banjar() {
    $rsk_banjar = DB::select("SELECT * FROM gmaps_geocache WHERE address LIKE '%RSK%' AND address LIKE '%banjar%'");

            $config['center'] = 'Plaza Telkom Banjar, Kota Banjar';
            $config['zoom'] = '16';
            $config['map_height'] = '500px';
            $config['geocodeCaching'] = true;
            $config['scrollwheel'] = false;                        
            GMaps::initialize($config);

            // add marker
            $marker['position'] = 'RSK Kantor Dinas Pendidikan Banjar;Jl. Gerilya Pamokolan, Kota Banjar';
            $marker['infowindow_content'] = 'Kantor Dinas Pendidikan Banjar';
            $marker['icon'] = 'https://chart.googleapis.com/chart?chst=d_map_spin&chld=0.4|0|FF0000|9|b|AP';
            GMaps::add_marker($marker);

            $map_banjar = GMaps::create_map();
            $rsk_banjar = GMaps::create_map();

            return view('data.data_rsk_ap.rsk_down_maps', 
                compact('map_banjar',
                        'rsk_banjar'));
}