Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Neha_26's avatar

ErrorException Undefined variable $categories (View: C:\xampp\htdocs\e-commerce\resources\views\includes\product\create.blade.php)

resources/views/includes/product/create.blade.php @include('product.index', ['categories' => $categories])

                @foreach($categories as $category)
                    <option value="{{ $category->id }}">{{ $category->name }}</option>
                @endforeach
            </select>
        </div>
        <div class="pm-form-group">
            <label for="productPrice">Price</label>
            <input type="number" id="productPrice" name="price" class="pm-input" required>
        </div>
        <div class="pm-form-group">
            <label for="category_id">Stock Status</label>
            <select id="category_id" name="category_id" class="pm-select" required>
                @foreach($categories as $category)
                    <option value="{{ $category->id }}">{{ $category->name }}</option>
                @endforeach
            </select>
        </div>
        <div class="pm-form-group">
            <label for="productImage">Image</label>
            <input type="file" id="productImage" name="image" class="pm-input" required>
        </div>
        <button type="submit" class="pm-button">Add Product</button>
    </form>
</div>
<main>
    @include('includes.navbar')

    @if(Auth::check())
        <h2 class="text-xl">Hello, {{ Auth::user()->first_name }}!</h2>

        @switch(Auth::user()->role)
            @case('vendor')
                <div class="main-content">
                    <!-- Charts -->
                    @include('includes.charts')

                    @include('includes.product.productTable')
                    
                    @include('includes.product.create')
                    
                    @include('includes.addCategory')
                    
                    @include('includes.addAttribute')

                    @include('includes.orderList')

                    <!-- Add more sections for other features here -->
                    @include('includes.otherFeatures')

                    @include('includes.customers')

                    @include('includes.marketingPromo')

                    <!-- New Campaign Modal -->
                    @include('includes.campaign')

                    <!-- New Promotion Modal -->
                    @include('includes.promotionModal')

                    @include('includes.settings')
                </div>
                @break

                @case('admin')
                    <!-- admin content -->
                @break
                @case('customer')
                    <!-- customer content -->
                @break

            @default
                <p class="text-red-500">Role not recognized. Please contact support.</p>
        @endswitch
    @else
        <p class="text-red-500">You are not logged in. Please <a href="{{ route('login') }}" class="text-blue-500">log in</a>.</p>
    @endif

    <form method="POST" action="{{ route('logout') }}" class="mt-4">
        @csrf
        <button type="submit" class="bg-red-500 text-white px-4 py-2 rounded">Logout</button>
    </form>
</main>

class CategoryController extends Controller { public function index() { $categories = Category::all(); // Fetch existing categories for the dropdown dd(compact('categories')); return view('includes.product.create', compact('categories')); } // Display categories form public function create() { $categories = Category::all(); // Fetch existing categories for the dropdown dd(compact('categories')); return view('includes.addCategory', compact('categories')); } // Store new category public function store(Request $request) { // Validate the incoming request data $request->validate([ 'category_name' => 'required|string|max:255', 'parent_category' => 'nullable|string|max:255', // Modify this based on your needs ]);

    // Create a new category instance
    $category = new Category();
    $category->category_name = $request->category_name;
    $category->parent_category = $request->parent_category; // Adjust this if you have a foreign key setup
    $category->save(); // Save the category to the database

    // Set a success message in the session
    Session::flash('success', 'Category added successfully!');

    // Redirect back to the dashboard or the same page
    return redirect()->back();
}

} Please help me. I'm a beginner and getting this "undefined variable" error.

0 likes
2 replies
Neha_26's avatar

@Sergiu17 Thanks for your solution. I tried this but it doesn't work. Maybe I'm doing something wrong. Please guide me.

public function index() { $categories = Category::all(); // Fetch existing categories for the dropdown dd(compact('categories')); // return view('includes.product.create', compact('categories')); return redirect()->back()->with(['categories' => Category::all()]); } // Display categories form public function create() { $categories = Category::all(); // Fetch existing categories for the dropdown dd(compact('categories')); // return view('includes.addCategory', compact('categories')); return redirect()->back(

Please or to participate in this conversation.