我在数据库中有3个不同的表,分别命名为 productdetails ,类别和 subcategorydetails ,同样,我还有3个不同的控制器 ProductdetailContoller , CategoryController 和 SubcategorydetailController 。 目前,我正在研究SubcategorydetailContoller的“视图”,它是 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>`
现在我想创建动态相关的下拉列表,但是在进行此操作之前,我无法将第二个表的数据显示到第二个下拉列表中。
这是我在控制器中正在做的事情:
namespace App\Http\Controllers;
use App\productdetail;
use App\category;
use App\subcategorydetail;
use Illuminate\Http\Request;
class SubcategorydetailController extends Controller
{
public function index()
{
$productdetails=productdetail::all();
return view('subcategory')->with('productdetail',$productdetails);
//i wrote this in another class but i still didn't get the desired output
$category=category::all();
return view('subcategory')->with('category', $category);
}
public function create()
{
}
public function store(Request $request)
{
$data=new subcategorydetail();
$data->SubCatType=$request->subcategory;
$data->CategoryID=$request->categoryid;
$data->save();
return dd($data);
//return view('subcategory');
}
更新1: 现在,我使用此代码在第二个下拉菜单中获得了数据
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,
]);
}
现在,在加入并建立逻辑上,当我从第一个下拉列表中选择特定产品然后在第二个下拉列表中仅显示所选产品的类别时,我就不会出错了。
更新2:
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,
]);
}
上面的代码显示此错误:
SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有一个错误;请参见语法。检查与您的MariaDB服务器版本相对应的手册,以在第1行的““ ProductID”?}“附近使用正确的语法(SQL:从categories
内部联接中选择CategoryType
。categories
{{1}上的productdetails
。categories
= ProductID
。productdetails
其中category.ProductID = {“ ProductID”:null})
答案 0 :(得分:0)
HTML不提供与您的注释有关的功能。
您可以执行以下操作:
在您的路线文件中
Route::get('{productDetail}', 'SubcategorydetailController@index');
根据您的控制器操作
public function index()
{
$productdetails=productdetail::all();
$category=category::where(...); // insert the logic to return only the categories that has anything to do with this product detail
return view('subcategory')->with([
'category' => $category,
'productDetail' => $productDetails,
]);
}