从数据库检索数据时出现未定义的错误Laravel

时间:2018-07-25 01:58:59

标签: php database laravel

嗨,我在控制器内添加新功能后,无法从数据库获取数据。谁能向我解释这个错误,我该怎么办?

我使用this video作为参考,并且想要做类似他的最终结果(请参见视频的1:09:28),但是我遇到了这个错误:

Error message

public function manageCategory() {
  $this->authCheck();
  $all_category = DB::table('category')->get();
  $manage = view('admin.pages.manage_category')->with('data', $all_category);
  return view('admin.pages.manage_category')->with('main_content', $manage);
}
<tbody>
  @foreach($data as $vData)
  <tr>
    <td><a href="#">{{ $vData->category_id }}</a></td>
    <td class="hidden-phone">{{ $vData->category_name }}</td>
    <td>{{ $vData->category_description }} </td>
    <td><span class="label label-important label-mini">{{ $vData->category_published }}</span></td>
    <td>
      <button class="btn btn-success"><i class="icon-ok"></i></button>
      <button class="btn btn-primary"><i class="icon-pencil"></i></button>
      <button class="btn btn-danger"><i class="icon-trash "></i></button>
    </td>
  </tr>
  @endforeach
</tbody>

5 个答案:

答案 0 :(得分:1)

您应该将代码更改为此

public function manageCategory() { 

   $this->authCheck();
   $all_category = DB::table('category')->get(); 

    return view('admin.pages.manage_category')->with('data',$all_category);
}

答案 1 :(得分:0)

尝试删除最后一行并按如下所示移动return关键字:

public function manageCategory() {
    $this->authCheck();
    $all_category = DB::table('category')
            ->get();
    return view('admin.pages.manage_category')
                ->with('data',$all_category);
}

我不明白为什么要调用2个视图。也许您或视频制作者犯了错

答案 2 :(得分:0)

您的foreach应该是这样的

RUN echo "oracle-java8-installer shared/accepted-oracle-license-v1-1 select true" | debconf-set-selections

这是因为在您的控制器中$ data包含类别,但是您已将数据分配给'$ manage'。再次将manage分配给另一个变量main_content

  @foreach($main_content['data'] as $vData)

答案 3 :(得分:0)

我不确定您要在此处做什么,但是此代码将帮助您解决错误。

public function manageCategory() {
    $this->authCheck();
    $all_category = DB::table('category')->get();

    view()->share('data',$all_category);
    view()->share('main_content',$manage);

    $manage = view('admin.pages.manage_category');
    return view('admin.pages.manage_category');
}

View类的Share方法将帮助您共享变量以进行查看。 祝你好运!

答案 4 :(得分:0)

I think this code is suite for you..

public function manageCategory() {
    $this->authCheck();
    $data = DB::table('category')
            ->get();
    return view('admin.pages.manage_category')
                    ->with('data', $data);  //Directly pass data to view file
}

In your blade file ....

    @if(!empty($data)) // add if
       @foreach($data as $vData)

                <tr>
                    <td><a href="#">{{ $vData->category_id }}</a></td>
                    <td class="hidden-phone">{{ $vData->category_name }}</td>
                    <td>{{ $vData->category_description }} </td>
                    <td><span class="label label-important label-mini">{{ $vData->category_published }}</span></td>
                    <td>
                        <button class="btn btn-success"><i class="icon-ok"></i></button>
                        <button class="btn btn-primary"><i class="icon-pencil"></i></button>
                        <button class="btn btn-danger"><i class="icon-trash "></i></button>
                    </td>
                </tr>
                @endforeach
            </tbody>
  @endif  ////add end if