davy_yg's avatar
Level 27

add(Cart $cart)

CartController.php

public function add(Cart $cart)
	{
    if (Products::checkStockAvailability($cart->prod_id, $cart->amount + 1)) {
        Cart::where('cart_id', $cart->cart_id)->update([
          'amount' => $cart->amount + 1
        ]);
    } else {
        flash('Pembelian melebihi jumlah stock yang tersedia')->error();
        return back();
    }

    return back();
	}

I also cannot understand what this means: add(Cart $cart) ?

Is it the same like $cart = new Cart; ?

0 likes
8 replies
bobbybouwmann's avatar

It means that the add method requires the $cart argument to be of type Cart. In this case, when no route model binding is used this will result in $cart = new Cart indeed.

This is called dependency injection. Laravel tries to resolve this class automatically. If you use route model binding, the binding from the URL is used. If there is no binding an empty model class will be used

Documentation: https://laravel.com/docs/7.x/routing#route-model-binding

davy_yg's avatar
Level 27

What do you mean by: the binding from the URL is used ?

I see that there is this url when you want to add to the product to cart: http://localhost/victoryeshop/public/cart/add/3

Then, my route is:

web.php

Route::post('/cart/add', 'CartController@store');

and I get this error:

(1/1) NotFoundHttpException

My onlineshop has a bug.

bobbybouwmann's avatar

Your URL is /cart/add/3, but your route is defined as cart/add. They don't match. You first need to figure that out.

I have the feeling that you have no idea what you're doing at this point. Just passing Cart $cart to the add method is not going to work in this situation. Can you show us where you call the add method?

davy_yg's avatar
Level 27

CartController.php

 public function store(Request $request)
{
    $this->validate($request, [
      'prod_id' => 'required|numeric',
      'amount' => 'required|numeric|min:1',
      'prod_type' => 'nullable'
    ]);

    $exist = Cart::where('prod_id', request('prod_id'))->where('prod_type', request('prod_type'))->where('user_id', auth()->user()->user_id)->first();

    if (empty($exist)) {
        if (Products::checkStockAvailability(request('prod_id'), request('amount'))) {
            Cart::create([
            'user_id' => auth()->user()->user_id,
            'prod_id' => request('prod_id'),
            'amount' => request('amount'),
            'prod_type' => request('prod_type'),
          ]);
        } else {
            flash('Pembelian melebihi jumlah stock yang tersedia')->error();
            return back();
        }
    } else {
        if (Products::checkStockAvailability(request('prod_id'), request('amount') + $exist->amount)) {
            Cart::where('cart_id', $exist->cart_id)->update([
              'amount' => $exist->amount + request('amount')
            ]);
        } else {
            flash('Pembelian melebihi jumlah stock yang tersedia')->error();
            return back();
        }
    }

    flash('Product dimasukkan ke dalam keranjang belanja');

    return back();
}

Then, it must be this one. Only request being passed.

I think I see some light. The add product only works through product details since you have select the t-shirt size. It does not work through the frontpage for some reason. since you only know the product_id and not the product_type (small, medium, large, x-large).

Any idea how to solve this problems?

Someone mentioned to create a pop up when you click the product picture asking to fill in the t-shirt size. How to create pop up like this?

Snapey's avatar

sorry, but you should not be working on stuff like this without building it for yourself from the ground up and learning all the required skills

drewdan's avatar

The add method you had up the top seems to not fit anywhere in the examples you have shown us.

You need to send the correct data to your request, and you are posting that data. So on the front page you would need a form to post the relevant data to the method on your controller.

I have no idea of the design you havbe adopted, but however you do it, you need to post the controller the information is requires:

You validate that this data is present:

'prod_id' => 'required|numeric',
'amount' => 'required|numeric|min:1',
'prod_type' => 'nullable'

However, you also use:

 'prod_type' => request('prod_type'),

In your create method on the Cart model.

With regards to a pop up or "modal" you could use jQuery or Vue to achieve this. You would need to look up how.

Snapey's avatar

@drewdan. its never his code. He is assigned 'fixing' code in other peoples projects, but he lacks the basic understanding and just asks questions here to get by. Its been like this for two years

Please or to participate in this conversation.