hira4472's avatar

Dynamically Dependent Drop-Down Lists Without AJAX & JQUERY

I'm not getting where I'm doing mistake in joining and establishing logic that when I selected the specific product from first drop down then it would show only categories of that selected product on second drop down??

public function index(Request $request)
    {

        $productdetail=productdetail::all();

        $data=new productdetail();
        $data=new productdetail();
        $data->ProductID=$request->productid;
        $category=category::where(DB::table('categories')
            ->join('productdetails','categories.ProductID','=','productdetails.ProductID')
             ->select('categories.CategoryType')
            ->whereRaw('categories.ProductID='.$data.'')
            ->get());

        return view('subcategory')->with([
            'productdetail'=>$productdetail,
            'category' => $category,

        ]);


    }

The above code showing this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"ProductID"?}' at line 1 (SQL: select categories.CategoryType from categories inner join productdetails on categories.ProductID = productdetails.ProductID where categories.ProductID={"ProductID":null})

0 likes
4 replies
ftiersch's avatar
->whereRaw('categories.ProductID='.$data.'')

where categories.ProductID={"ProductID":null})

I'm not sure what you are trying to accomplish here exactly?! But an Eloquent Model will automatically be converted to JSON if you force it to a string (which you are doing in that where) and the result is what you see in the error which is not MySQL

hira4472's avatar

I'm trying to show only specific options on second drop-down depended on the selection of first drop-down

ftiersch's avatar

And you would reload the page every time someone changes the selection of the first drop down or what is your plan?

->whereRaw('categories.ProductID='.$data.'')

->where('categories.ProductID=' . $request->get('productid'))

Change the first one to the second one, that should at least get rid of your SQL error. But I think there is more wrong with that plan.

hira4472's avatar

I have 3 different tables in database named productdetails,categories & subcategorydetails and likewise I have 3 different Controllers ProductdetailContoller,CategoryController & SubcategorydetailController respectively. Currently, i'm working on SubcategorydetailContoller's 'view' which is subcategory.blade.php

`

            @csrf
            <div class="form-group">

              <select name="productid">


                <option value="select product">
                @foreach ($productdetail as $row)
                  <option value="{{$row->ProductID}}">
                    {{$row->ProductType}}
                  </option>
                  @endforeach
                  </option>
              </select>

              <select name="categoryid">


                <option value="select category">
                @foreach ($category as $row)
                  <option value="{{$row->CategoryID}}">
                    {{$row->CategoryType}}

                  </option>
                  @endforeach
                  </option>
              </select>

              <input type="text" name="subcategory"/>

              <input type="submit" value="add category"/>
            </div>
          </form>
        </div>`

Now, i want to make dynamically dependent drop-down lists. I got the data on second drop down using this code:

 public function index()
    {

        $productdetail=productdetail::all();
     
        $category=category::all();

            /*$category=category::where(DB::table('categories')
            ->join('productdetails','categories.ProductID','=','productdetails.ProductID')
                ->select('categories.CategoryType')
            //->whereRaw('categories.ProductID="1"')
            ->get());*/
            return view('subcategory')->with([
                'productdetail'=>$productdetail,
                'category' => $category,

        ]);


    }

Now I'm not getting where I'm doing mistake in joining and establishing logic that when I selected the specific product from first drop down then it would show only categories of that selected product on second drop down??

public function index(Request $request)
    {

        $productdetail=productdetail::all();

        $data=new productdetail();
        $data=new productdetail();
        $data->ProductID=$request->productid;
        $category=category::where(DB::table('categories')
            ->join('productdetails','categories.ProductID','=','productdetails.ProductID')
             ->select('categories.CategoryType')
            ->whereRaw('categories.ProductID='.$data.'')
            ->get());

        return view('subcategory')->with([
            'productdetail'=>$productdetail,
            'category' => $category,

        ]);


    }

The above code showing this error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '"ProductID"?}' at line 1 (SQL: select categories.CategoryType from categories inner join productdetails on categories.ProductID = productdetails.ProductID where categories.ProductID={"ProductID":null})

Please or to participate in this conversation.