How to use an if statement on the blade template. error: Undefined variable $category

I have created a function to retrieve a specific column in the database using eloquent, on my productsController

public function create()
        $category = Product::select('category')->get();

        return view('products.create');

And Now I want to use an if statement on the create.blade.php to check whether the value in the database is equal to ‘liquor’,

    <div class="row mb-4">
                <label for="category" class="col-md-4 col-form-label text-md-end">Category</label>
                <div class="col-md-6">
                    <select name="category" id="category" class="form-select col-md-6">
                        <option value="liquor">liquor</option>
                        <option value= "food">food</option>
                        <option value="DIY">DIY</option>
                        <option value= "thrift shops">thrift shops</option>
                        <option value= "home decor">home decor</option>
                        <option value= "phones and tablets">phones and tablets</option>
                        <option value= "computing">computing</option>
                        <option value= "electronics">electronics</option>
                        <option value= "beauty products">beauty products</option>
                        <option value= "others">others</option>

        @if($category == 'liquor')

            <div class="row mb-4">
                <label for="subcategory" class="col-md-4 col-form-label text-md-end">Sub Category</label>
                <div class="col-md-6">
                    <select name="subcategory" id="subcategory" class="form-select col-md-6">
                        <option value="null">Select subcategory</option>
                        <option value="wine">Wine</option>
                        <option value="whisky">Whisky</option>
                        <option value="brandy">Brandy</option>
                        <option value="scotch">Scotch</option>
                        <option value="spirit">Spirit</option>
                        <option value="gin">Gin</option>
                        <option value="vodka">Vodka</option>
                        <option value="beer">Beer</option>
                        <option value="rum">Rum</option>
                        <option value="mixers">Mixers</option>
                        <option value="bourbon">Bourbon</option>
                        <option value="cognac">Cognac</option>
                        <option value="other">Other</option>
            <div class="row mb-4">
                <label for="volume" class="col-md-4 col-form-label text-md-end">Volume</label>
                <div class="col-md-6">
                    <select name="volume" id="volume" class="form-select col-md-6">
                        <option value="null">Choose volume</option>
                        <option value="5ltr">5ltr</option>
                        <option value="1ltr">1ltr</option>
                        <option value="750ml">750ml</option>
                        <option value="500ml">500ml</option>
                        <option value="250ml">250ml</option>
                        <option value="other">Other</option>

However, I get the following error: Undefined variable $category

What could be the issue?

>Solution :

I think you forgot to send the $category to the view:

return view('products.create', compact("category"));

or like this:

return view('products.create')
            ->with('category', $category)
            ->with('title', 'New Title');

or like this:

return view('products.create', ['category' => $category]);

You can add as many vars/collections/or whatever as you wish both ways.

Leave a Reply