irankhosravi
4 months ago

How to order a product only once in laravel?

Posted 4 months ago by irankhosravi

A user cannot bid on a order more than once. If the user clicks on the order again, he should be prompted with a response page noting the user of having previously bid on the order.

OrderController.php

public function store(Request $request)
{
    $this->validate(request(), [
        'category_id' => 'required'
    ]);

    $order = Order::findOrFail(request('category_id'));

    if (auth()->user()->checkOrder($order)) {
        alert()->error('Warning', 'You ordered already');
        return redirect()->back();
    }
}

User.php

public function checkOrder($category)
{
    return !! Order::where('user_id' , $this->id)->where('category_id' , $category->id)->first();
}

Please sign in or create an account to participate in this conversation.