Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

JuanDMeGon/Laravel-from-Scratch

17 stars
2 code files
View JuanDMeGon/Laravel-from-Scratch on GitHub

app/Services/CartService.php

Open in GitHub
use App\Cart;
use Illuminate\Support\Facades\Cookie;
 
class CartService
{
protected $cookieName;
protected $cookieExpiration;
 
public function __construct()
{
$this->cookieName = config('cart.cookie.name');
$this->cookieExpiration = config('cart.cookie.expiration');
}
 
public function getFromCookie()
{
$cartId = Cookie::get($this->cookieName);
 
return Cart::find($cartId);
}
//
}

app/Http/Controllers/CartController.php

Open in GitHub
use App\Services\CartService;
 
class CartController extends Controller
{
public $cartService;
 
public function __construct(CartService $cartService)
{
$this->cartService = $cartService;
}
 
public function index()
{
$cart = $this->cartService->getFromCookie();
 
return view('carts.index')->with([
'cart' => $cart,
]);
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.