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

问题描述 投票:0回答:2

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

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

laravel laravel-5 laravel-5.4 laravel-5.5
2个回答
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

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

© www.soinside.com 2019 - 2024. All rights reserved.