【发布时间】:2020-06-29 22:32:42
【问题描述】:
我在名为 productdetails、categories 和 subcategorydetails 的数据库中有 3 个不同的表,同样我有 3 个不同的控制器 ProductdetailContoller em>,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:select categories.CategoryType from categories inner join productdetails on @987654329 @.ProductID = productdetails.ProductID where categories.ProductID={"ProductID":null})
【问题讨论】:
-
//i wrote this in another class but i still didn't get the desired output对此的预期输出是什么?首先我们需要解决第一个问题才能进入第二个问题 -
当我在第一个下拉菜单中选择产品时,它应该在第二个下拉菜单中显示该特定产品的类别。
-
要么一次返回所有结果,要么在用户从第一个下拉列表中选择内容时发出 ajax 请求以获取第二个下拉列表的选项。您将需要两种方式的 javascript,而您还没有编写任何代码。尝试使用 javascript 并编辑问题
-
好吧,我必须在不使用 ajax 或 jquery 的情况下这样做。
-
HTML 本身不提供此类选项。您将需要 javascript 来控制流程。一种解决方案是:在您的路线上添加
{productDetail},在控制器上,您只需要获取与该产品详细信息$category=category::where(...); // use the productDetail in here有任何关系的类别
标签: php laravel laravel-5 eloquent laravel-4