首页 > 解决方案 > ErrorException (E_NOTICE) 未定义的偏移量:0 laravel

问题描述

我正在尝试从数据库中读取数据并将数据显示到表单中,但我不断收到此错误。

这是我的控制器:

public function create()
{
    /* this function gets data from the database (marks table) and render it to the view view */

    $data['data']=DB::table('marks')->get();

    if(count($data[0])>0){
        return view('view',$data);
    }
    else{
        return view('view');
    }   
}

这就是我定义路线的方式:

Route::resource('claude', 'viewcontroller');

标签: databaselaravelundefinedoffset

解决方案


该变量$data没有索引0
但它有一个名为data.
所以你必须通过密钥访问它。

它应该是

 if(count($data['data']) > 0){
     return view('view',$data);
 }

推荐阅读