1

I’m working on category and sub_catengory with laravel select menu related to shopping site using Mass Assignment, I’m having trouble to retrieve from the database from the select menu. I'm getting error said

Undefined variable: categories_dropdown (View:

add_product.blade.php file.

<div class="row">
       <div class="col-md-6">
           <div class="form-group row">
                 <label for="Category" class="col-sm-3 text-right control-label col-form-label font- 
                 weight-bold">Category: <span class="red_star">&#8727;</span></label>
                     <div class="col-sm-9">
                          <select name="category" id="category" class="custom-select">
                             <?php echo $categories_dropdown; ?>
                          </select>
                     </div>
            </div>
 </div>

ProductController.php

public function store(){

        $data = request()->validate([
            'sku' => 'required',
            'product_name' => 'required',
            'description' => 'required',
            'brand' => 'required',
            'category_id' => 'required',
            'sub_categories' => 'required',
'size' => '',
            'status' => '',
            'product_code' => '',
            'care' => '',

        ]);

        Product::create($data);

$categories = Category::where(['parent_id'=>0])->get();
$categories_dropdown = "<option value='' selected disabled>Select</option>";
foreach($categories as $cat){
    $categories_dropdown .= "<option value='".$cat->id."'>".$cat->name."</option>";
    $sub_categories = Category::where(['parent_id'=>$cat->id])->get();
    foreach ($sub_categories as $sub_cat) {
        $categories_dropdown .= "<option value = '".$sub_cat->id."'>&nbsp;--&nbsp;".$sub_cat->name."</option>";
    }
}

//Categories drop down end

return view('admin.products.add_product')->with(compact('categories_dropdown'));
}

product.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $guarded = [];
}

what did I missed it! many thanks.

1
  • You do that all in store() function. You need another function to do that. Commented Jul 2, 2020 at 18:12

2 Answers 2

1

You have to change this

return view('admin.products.add_product',compact('categories_dropdown'));
Sign up to request clarification or add additional context in comments.

Comments

0

I am always using this when I pass data to view:

...
return view('admin.products.add_product')->with([
    'categories_dropdown'    => $categories_dropdown
]);

4 Comments

Yes, I have tried that both above and I'm still getting the same error message said "$categories_dropdown is undefined" any idea please!
Oh... You are using this in store. Don't return view, return redirect()->with();
I have tried with "return redirect" and I'm still getting the same error message, and also on bottom of errors said to try with {{ $categories_dropdown ?? '' }} what is this mean by questions mark like this ?? ..!
?? this means - if $categories_dropdown is null or undefined, to display ''

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.