1

I have a file called create.blade.php inside directory resources/views/product/create.blade.php.

I am trying to view the create.blade.php file inside index.blade.php that is located in resources/views/product/index.blade.php using include @include('product.create').

The issue is that when I try to call the view, I get the error Undefined variable

below is the productController class:

  public $barcode_types;

    /**
     * Constructor
     *
     * @param ProductUtils $product
     * @return void
     */
    public function __construct(ProductUtil $productUtil, ModuleUtil $moduleUtil)
    {
        $this->productUtil = $productUtil;
        $this->moduleUtil = $moduleUtil;

        //barcode types
        $this->barcode_types = $this->productUtil->barcode_types();
    }

public function create()
    {
        if (!auth()->user()->can('product.create')) {
            abort(403, 'Unauthorized action.');
        }

        $business_id = request()->session()->get('user.business_id');

        //Check if subscribed or not, then check for products quota
        if (!$this->moduleUtil->isSubscribed($business_id)) {
            return $this->moduleUtil->expiredResponse();
        } elseif (!$this->moduleUtil->isQuotaAvailable('products', $business_id)) {
            return $this->moduleUtil->quotaExpiredResponse('products', $business_id, action('ProductController@index'));
        }

        $categories = Category::forDropdown($business_id, 'product');

        $brands = Brands::forDropdown($business_id);
        $units = Unit::forDropdown($business_id, true);

        $tax_dropdown = TaxRate::forBusinessDropdown($business_id, true, true);
        $taxes = $tax_dropdown['tax_rates'];
        $tax_attributes = $tax_dropdown['attributes'];

        $barcode_types = $this->barcode_types;
        $barcode_default =  $this->productUtil->barcode_default();

        $default_profit_percent = request()->session()->get('business.default_profit_percent');;

        //Get all business locations
        $business_locations = BusinessLocation::forDropdown($business_id);

        //Duplicate product
        $duplicate_product = null;
        $rack_details = null;

        $sub_categories = [];
        if (!empty(request()->input('d'))) {
            $duplicate_product = Product::where('business_id', $business_id)->find(request()->input('d'));
            $duplicate_product->name .= ' (copy)';

            if (!empty($duplicate_product->category_id)) {
                $sub_categories = Category::where('business_id', $business_id)
                        ->where('parent_id', $duplicate_product->category_id)
                        ->pluck('name', 'id')
                        ->toArray();
            }

            //Rack details
            if (!empty($duplicate_product->id)) {
                $rack_details = $this->productUtil->getRackDetails($business_id, $duplicate_product->id);
            }
        }

        $selling_price_group_count = SellingPriceGroup::countSellingPriceGroups($business_id);

        $module_form_parts = $this->moduleUtil->getModuleData('product_form_part');
        $product_types = $this->product_types();

        $common_settings = session()->get('business.common_settings');
        $warranties = Warranty::forDropdown($business_id);

        //product screen view from module
        $pos_module_data = $this->moduleUtil->getModuleData('get_product_screen_top_view');

        return view('product.create')
            ->with(compact('categories', 'brands', 'units', 'taxes', 'barcode_types', 'default_profit_percent', 'tax_attributes', 'barcode_default', 'business_locations', 'duplicate_product', 'sub_categories', 'rack_details', 'selling_price_group_count', 'module_form_parts', 'product_types', 'common_settings', 'warranties', 'pos_module_data'));
    }

What am I doing wrong here?

are there further steps or configuration to be done before calling the view?

Code
  • 71
  • 6
  • you don't have `$barcode_types` variable. Better post your controller too. – Wahyu Kristianto Feb 06 '22 at 15:33
  • @WahyuKristianto I have edited the question and added the controller code, sorry for the mistake. Please let me know if further details would be required – Code Feb 06 '22 at 15:37
  • @WahyuKristianto I have further edited the question to add the code where the barcode types seems to be initialized – Code Feb 06 '22 at 15:44
  • But your view is `view('product.create')` not `index` – Wahyu Kristianto Feb 06 '22 at 16:11
  • Looks like you do have "create" method. And you want to insert the view in the "index", without passing the controller method "create" right? – Wahyu Kristianto Feb 06 '22 at 16:13
  • @WahyuKristianto yes right – Code Feb 06 '22 at 16:14
  • @WahyuKristianto I want to call the `product.create` view inside `index.blade.php` – Code Feb 06 '22 at 16:15
  • You cannot include another view when that view requires a controller (which has variables for that view). My suggestion is, you make a [component](https://laravel.com/docs/8.x/blade#components) for it. – Wahyu Kristianto Feb 06 '22 at 16:17
  • 1
    @WahyuKristianto The issue is becoming clearer now, will into the component creation solution. Thank you! – Code Feb 06 '22 at 16:31
  • @WahyuKristianto for the `barcode_types` issue, will storing its value in a session and later retrieving it inside the `create.blade.php` file resolve it? – Code Feb 06 '22 at 17:06
  • @WahyuKristianto because when I removed some of the lines inside `create.blade.php`, I managed to get the view to load – Code Feb 06 '22 at 17:07

0 Answers0