Search code examples
laravellaravel-5laravel-5.4laravel-5.5

Undefined variable in Blade (Can't get Solution from other answer)


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');
});

Controller:

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);
    }

Blade:

@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

Data saved successfully but can't fetch from DB. Actually I have found all of the question in this related, but didn't get my answer. Please help me. I am trying a lot of times. Thanks in advance.


Solution

  • Try with this:

    @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