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})