Blade中的未定义变量(无法从其他答案获得解决方案)

时间:2018-04-11 12:31:04

标签: laravel laravel-5 laravel-5.4 laravel-5.5

Web.PHP:

Route::group(['prefix' => 'admin', 'middleware' => ['auth' => 'admin']], function ()
{
    Route::get('/', 'AdminController@index');
    Route::get('/ecommerce/addCategory', 'AdminProductController@addCategory');
    Route::get('/ecommerce/showCategory', 'AdminProductController@showCategory');
    Route::post('/ecommerce/saveCategory', 'AdminProductController@saveCategory');
});

控制器:

public function showCategory()
    {
        $sidebar = view('admin.sidebar.sidebar');
        $content = view('admin.ecommerce.showCategory');


        $category = DB::table('categories')->get();

        return view('admin.ecommerce.dashboard', compact('category'))
            ->with('sidebar', $sidebar)
            ->with('content', $content);
    }

刀片:

@foreach($category as $cat)

                        <tr>
                            <td>{{ ++$i }}</td>
                            <td>{{ $cat->categoryName }}</td>
                            <td>{{ $cat->categoryDescription }}</td>
                            <td>{{ $cat->categoryStatus = 1 ? 'Published' : 'Unpublished' }}</td>

                            <td>
                                <a href="#" class="table-action-btn"><i class="md md-edit"></i></a>
                                <a href="#" class="table-action-btn"><i class="md md-close"></i></a>
                            </td>
                        </tr>

                        @endforeach

数据已成功保存,但无法从数据库中获取。其实我已经找到了相关的所有问题,但没有得到我的答案。请帮我。我正在尝试很多次。提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

@foreach(App\YourModel::all() as $cat)
  <tr>
     <td>{{ ++$i }}</td>
     <td>{{ $cat->categoryName }}</td>
     <td>{{ $cat->categoryDescription }}</td>
     <td>{{ $cat->categoryStatus = 1 ? 'Published' : 'Unpublished' }}</td>

     <td>
         <a href="#" class="table-action-btn"><i class="md md-edit"></i></a>
         <a href="#" class="table-action-btn"><i class="md md-close"></i></a>
     </td>
   </tr>
@endforeach

答案 1 :(得分:0)

尝试更改此内容:

  return view('admin.ecommerce.dashboard', compact('category'))
            ->with('sidebar', $sidebar)
            ->with('content', $content);

收件人:

return view('admin.ecommerce.dashboard',['category' => $category, 'content' => $content, 'sidebar' => $sidebar]);

您也可以查看以下内容: https://laravel.com/docs/5.5/views#passing-data-to-views