Nafeeur Rahman
Nafeeur Rahman

Reputation: 67

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.

Upvotes: 0

Views: 74

Answers (2)

Hesham AbuSaif
Hesham AbuSaif

Reputation: 84

Try to change this:

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

To:

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

You also can check this out: https://laravel.com/docs/5.5/views#passing-data-to-views

Upvotes: 0

Mr. Perfectionist
Mr. Perfectionist

Reputation: 2746

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

Upvotes: 1

Related Questions