0

I'm working on Laravel and got stuck in this mess of undefined variable $category, I don't know why and where is the exact problem.

I have done this much.

AdminAjaxController

public function category()
{
    $category=DB::select('select category_name,category_id from categories');
    return view('admin.category_table',compact('category'));
}

category_table View

<table id="category" class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Category Name</th>
            <th>Delete</th>
            <th>Update</th>
      </tr>
    </thead>
    <tbody>
  @foreach($category as $value)
    <tr>
        <td>{{ $value->category_id}}</td>
        <td>{{ $value->category_name}}</td>
        <th>Delete</td>
        <td>Update</td>
    </tr>
    @endforeach
</tbody>
</table>

3 Answers 3

1

You should try this solution:

public function category()
{
    $category=DB::select('select category_name,category_id from categories');
    return view('admin.category_table')->with(['category' => $category]);
}

In addition to this you are getting this error, because you do not send your category variable.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

public function category()
{
    $category=DB::table('categories')->select('category_name', 'category_id')->get();

    return view('admin.category_table',compact('category'));
}

2 Comments

still the same thing.. :(
This have to work! please follow the steps 1. make sure the page is .blade.php 2. make sure you are in admin folder 3. run php artisan cache:clear 4. if still the problem give a screenshoot here
0

Try this, it will work.

public function category()
{
    $category=
      DB::select('select category_name,category_id from categories');
    return view('admin.category_table',[ 'category'=>$category ]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.