mohammadv184/laravel-cart

Laravel 框架的购物车

v2.3.2 2021-07-12 16:30 UTC

This package is auto-updated.

Last update: 2024-09-29 05:56:44 UTC


README

Latest Stable Version Total Downloads Monthly Downloads Build Status License

为 Laravel 框架提供的购物车

安装

通过 Composer 安装此包。

从终端运行 Composer require 命令

composer require mohammadv184/laravel-cart

如果您使用的是 Laravel 5.5,这就足够了。

如果您仍然使用 Laravel 5.4 版本,您需要执行以下最终步骤:添加包的服务提供者和别名。要做到这一点,请打开您的 config/app.php 文件。

providers 数组中添加新行

Mohammadv184\Cart\CartProvider::class

可选地,在 aliases 数组中添加新行

'Cart' => Mohammadv184\Cart\Facades\Cart::class,

现在您已准备好开始在您的应用程序中使用 laravel-cart。

数据库

要将购物车保存到数据库,该包需要知道表的名称。默认情况下,该包将使用名为 cart_items 的表。如果您想更改这些选项,您必须发布 config 文件。

php artisan vendor:publish --provider="Mohammadv184\Cart\CartServiceProvider" --tag="config"

这将为您提供一个 cart.php 配置文件,您可以在其中进行更改。

为了使您的生活更加方便,该包还包含一个可立即使用的 migration,您可以通过运行以下命令发布它:

php artisan vendor:publish --provider="Mohammadv184\Cart\CartServiceProvider" --tag="migrations"

这将把 cart_items 表的迁移文件放入 database/migrations 目录。现在您只需运行 php artisan migrate 来迁移您的数据库。

概述

查看以下主题之一以了解有关 Laravel-cart 的更多信息

用法

laravel-cart 为您提供了以下方法来使用

Cart::put()

将项目添加到购物车非常简单,您只需使用 put() 方法,该方法接受多种参数。

在其最基本的形式中,您可以指定要添加到购物车的产品的数量、价格和产品模型。

$product=Product::find(1);
Cart::put([
    "price"=>10,
    "quantity"=>1
],$product);

Cart::update()

要更新购物车中的项目,您首先需要项目的 rowId。然后您可以使用 update() 方法来更新它。

如果您只想更新数量,您只需将产品模型或 ID 和新数量传递给更新方法即可

$product=Product::find(1);
Cart::update(2,$product); // Will update the quantity

或者

$id = 'Biuwla5871';
Cart::update($id, 2); // Will update the quantity

如果您想更新项目的更多属性,您可以传递一个数组作为第一个参数给更新方法。这样,您可以使用给定的 ID 更新项目的所有信息。

Cart::update(['price' => 1000],"16cdac2cs8"); // Will update the price
$product=Product::find(1);
Cart::update(['id'=>"1c6a4c6a75",'price'=>1000], $product); // Will update the id and price

Cart::delete()

要删除购物车中的项目,您需要再次提供产品模型或 ID。您只需将此 ID 传递给 delete() 方法,它就会从购物车中删除该项目。

$product=Product::find(1);
Cart::delete($product);

或者

$id = 'da39a3ee5e';
Cart::delete($id);

Cart::get()

如果您想通过其 ID 或产品模型获取购物车中的项目,您只需在购物车上调用 get() 方法,并传递 ID 或产品模型即可。

$product = Product::find(1);
Cart::get($product);

或者

$id = 'da39a3ee5e';
Cart::get($id);

Cart::all()

当然,您也希望获取购物车中的所有项目。这就是您将使用 all 方法的地方。此方法将返回一个包含购物车项目的 Collection。

Cart::all();

Cart::flush()

如果您想完全删除购物车中的所有项目,您可以在购物车上调用 flush 方法。这将删除当前购物车实例中的所有购物车项目。

Cart::flush();

Cart::totalPrice()

totalPrice() 方法可以用来获取购物车中所有项目的总价。

Cart::totalPrice();

示例

以下是如何在表中列出购物车内容的示例

// Add some items in your Controller.
$product=Product::find(1);
Cart::put(['quantity'=>1,'price'=>9.99],$product);
$product=Product::find(2);
Cart::put(['quantity'=>2,'price'=>10],$product);
// Display the content in a View.
<table>
   	<thead>
       	<tr>
           	<th>Product</th>
           	<th>quantity</th>
           	<th>Price</th>
        </tr>
   	</thead>
   	<tbody>
   		@foreach(Cart::all() as $row)
       		<tr>
           		<td>
               		<p><strong>{{$row->product->name}}</strong></p>
               	</td>
           		<td><p>{{$row->quantity}}</p></td>
           		<td>${{$row->price}}</td>
       		</tr>
	   	@endforeach
   	</tbody>
   	
   	<tfoot>
   		<tr>
   			<td colspan="2">&nbsp;</td>
   			<td>Total</td>
   			<td>{{Cart::totalPrice()}}</td>
   		</tr>
   	</tfoot>
</table>