Laravel 获取所有相同的id

Laravel get all with same id

我有一个 table 和 category_id。 Category_id 可以有 1、2、3 或 4 作为值。现在我想显示所有值为 1 的结果。在这段代码中我该怎么做?谢谢

public function getshopGSM()
{
    $shopGSM = new Product();
    $shopGSM = Product::all();
    return view('eindwerk.shopGSM', [
        'shopGSM' => $shopGSM
    ]);
}
public function getshopGSM()
{
    $shopGSM = Product::where('category_id', 1)->get();
    return view('eindwerk.shopGSM', [
        'shopGSM' => $shopGSM
    ]);
}       

像这样尝试:

public function getshopGSM()
    {
        $shopGSM = Product::where('category_id', 1)->get();
        return view('eindwerk.shopGSM', compact('shopGSM '));
    }

您必须 select 所有产品,其中 category_id 为 1,并且您需要插入 ->get() 因为您需要获取所有结果。

if you want select only one you can use ->first()

你也可以 select 产品并给他们一个订单,你可以使用

Product::orderBy('id','desc')->where('category_id', 1)->get();