I am trying to develop a Laravel-based E-Commerce for a university project, nothing too fancy, it won't be released.
Premise: I am a student and a beginner, I apologize for any errors of mine. I am using Laravel 6.15.
I have made a very basic HTML+CSS template and built a custom database for the e-comm, loaded up a few records just for testing purposes: everything works (products are displayed, data is correct, no errors). I have to build a cart in order to let the end user buy stuff, but despite trying several packages, I can't add items to my cart.
Here's what I tried:
My code is as follows (as of last update, so here I'm using Treestoneit's Cart).
CartController.php
use Illuminate\Http\Request;
use App\Product;
use Treestoneit\ShoppingCart\Facades\Cart;
...
public function store(Request $request)
{
$product = Product::findOrFail($request->id);
Cart::add($product, 1);
return redirect()->route('cart.index')->with('success_message', 'Item added to cart successfully!');
}
product.blade.php
<form action="{{ route('cart.store', $product) }}" method="POST">
{{ csrf_field() }}
<button type="submit" class="btn btn-primary">Add to cart!</button>
</form>
($product is being correctly retrieved, no issues with that, here it's being sent to the store() function above)
cart.blade.php
...
{{ Cart::items() }}
...
@if (session()->has('success_message'))
{{ session()->get('success_message') }}
@endif
@if (count($errors) > 0)
@foreach ($errors->all() as $error)
<p>{{ $error }}</p>
@endforeach
@endif
(before switching to the latest package I was using a @foreach statement to print out the whole cart item by item, but decided to make it easy in order to avoid any grammar error, here Cart::items() should just print the cart content, but it's empty: '[]').
Notes:
At this point I have no idea what I'm getting into, and I'm trying to understand whether I'm missing something or there's an actual problem. Can anyone help or suggest a compatible package? Thanks in advance.
we are creators of the TreeStoneIT Shopping Cart for Laravel.
It's hard to know for certain since there is no full source to reproduce the issue, my guess would be that the Model with Buyable
trait might have not been setup correctly, but can as well be something else.
You can see a full working example here: https://github.com/treeStoneIT/laravel-commerce