Wesley
Wesley

Reputation: 93

Laravel get all with same id

I have a table with category_id. Category_id can has 1,2,3 or 4 as a value. Now I want to show all results with value 1. How do i do it in this code? Thanks.

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

Upvotes: 0

Views: 5512

Answers (2)

Diego Cespedes
Diego Cespedes

Reputation: 1353

try like this:

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

You must select all products WHERE category_id is 1, and you need insert ->get() because you need get all results.

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

also you can you can select products and give them a ORDER, you can use

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

Upvotes: 0

IllegalPigeon
IllegalPigeon

Reputation: 1769

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

Upvotes: 1

Related Questions