试图从数据库返回视图,但得到“未定义的变量错误-Laravel 5.2”

时间:2018-06-29 07:46:40

标签: laravel

控制器,路由和查看代码如下。 遇到错误

试图从数据库返回视图,但得到-“未定义的变量错误-Laravel 5.2”

Football.blade.php

@if (isset($football_datas)) 
    @foreach($football_datas as $football_data)    
        {{$football_data->day}}      
        </h3>

        <div style="height:20px;">
            <p class="time-identity" > 14:00</p> 
            <a href="{{Route('stream')}}"    > 
                <p class="match-identity">{{$football_data->country}} vs {{$football_data->country}}</p>
                <p  class="live-video-identity"> video </P>
            </a>
        </div>
    @endforeach
@endif

football_dataController.php

class football_datacontroller extends controller
{      
    public function index(){
        $football_datas= DB::table('football_datas')->select('id','country','day')->get();


        return view('football',['football_datas'=>$football_datas]);
    }
}

路线

Route::post('football', 'football_dataController@index'); 

3 个答案:

答案 0 :(得分:1)

您可以在Controller上尝试这个吗?

    return view('football',compact('football_datas'));

答案 1 :(得分:0)

您的代码

return view('football',['football_datas'=>$football_datas]);

尝试

return View::make('football')->with('football_datas', $football_datas);

希望对您有帮助。

答案 2 :(得分:0)

您可以将这种方式用于变量,因此您不需要对它们进行所有的custon更改:

class football_datacontroller extends controller
{      
    public function index(){
        $football_datas= DB::table('football_datas')->select('id','country','day')->get();

        $vars['football_datas'] = $football_datas;
        return view('football', $vars);
    }
}

然后,您可以在彼此之上添加几个“ $ vars ['blabla]'= $ blabla”,并且所有变量都将仅在视图中使用{{$ football_datas}}或{{$ blabla} }中提供的示例。

下面是有关如何拥有几个的示例:

class football_datacontroller extends controller
{      
    public function index(){
        $football_datas= DB::table('football_datas')->select('id','country','day')->get();

        $vars['blabla'] = $blabla;
        $vars['football_datas'] = $football_datas;
        return view('football', $vars);
    }
}