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 >
<label for="category" >Category</label>
<div >
<select name="category" id="category" >
<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>
</select>
</div>
</div>
@if($category == 'liquor')
<div >
<label for="subcategory" >Sub Category</label>
<div >
<select name="subcategory" id="subcategory" >
<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>
</select>
</div>
</div>
<div >
<label for="volume" >Volume</label>
<div >
<select name="volume" id="volume" >
<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>
</select>
</div>
</div>
@endif
However, I get the following error: Undefined variable $category
What could be the issue?
CodePudding user response:
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.