【发布时间】:2023-03-03 23:54:02
【问题描述】:
我正在尝试从数据透视表中获取产品名称,但不幸的是我不知道如何从数据透视表中获取产品名称,请帮助我,谢谢。
产品型号
public function category()
{
return $this->belongsToMany('App\ProductCategory', 'product_category', 'product_id', 'mf_product_category_id');
}
产品类别模型
public function products()
{
return $this->belongsToMany('App\Product', 'product_category', 'mf_product_category_id', 'product_id');
}
控制器
public function getproduct(Request $request)
{
// getting category Id
$categoryId = $request->category;
// getting product Id
$name = trim($request->product);
$productId = Product::where('name', $name)->pluck('id');
$getProductcategory = ProductCategoryCount::whereIn('mf_product_category_id', $categoryId)->whereIn('product_id', $productId)->get();
return $getProductcategory;
// return response()->json($getproduct);
}
【问题讨论】:
标签: laravel