Undefined variable: blogcat (View: /home/techpriest/joseph/resources/views/admin/view_category.blade.php)

Undefined variable: blogcat (View: /home/techpriest/joseph/resources/views/admin/view_category.blade.php) this is my error message in laravel 8 but when I look at my code all seems well.
here is my code.


public function addBlogCat (Request $request){
        if ($request->isMethod('post')) {
            $data = $request->all();
            $blogcat = new Foliocategories;
            $blogcat->name = $data['category_name'];
            return redirect ('/blog/categories');
        return view ('admin.view_category');
    public function viewBlogCat (){
        $blogcat = DB::select('select * from foliocategories');
        return view ('admin.view_category', ['blogcat'=>$blogcat]);

And here is my view;
    @foreach($blogcat as $blogcat)
                                        <td>{{ $blogcat->id }}</td>
                                        <td>{{ $blogcat->name }}</td>
                                        <td>{{ $blogcat->created_at }}</td>
                                        <td><span class="badge badge-danger">Due</span></td>
                                        <td class="action h4">
                                            <div class="table-action-buttons">
                                                <a class="edit button button-box button-xs button-info" href="#"><i class="zmdi zmdi-edit"></i></a>
                                                <a class="delete button button-box button-xs button-danger" href="#"><i class="zmdi zmdi-delete"></i></a>


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I found two issues here. In the addBlogCat action, you don’t inject any blog category. The viewBlogCat is not bad, but in the view, you are using a loop with a model instance.

What about rewrite the code to this, lets use full meaningful variable names:

public function addBlogCategory(Request $request)
    if ($request->isMethod('post')) {
        $data = $request->all();
        Foliocategories::create(['name' => $request->input('category_name')]);

            return redirect ('/blog/categories');

    return view ('admin.view_category', [
        'blogCatogories' => Foliocategories::all();
public function viewBlogCategories()
    $blogCategories = Foliocategories::all();
    return view ('admin.view_category', compact('blogCategories'));

Then in the view:

@foreach($blogCategories as $blogCategory)
        <td>{{ $blogCategory->id }}</td>
        <td>{{ $blogCategory->name }}</td>
        <td>{{ $blogCategory->created_at }}</td>

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x