如何使用 DarrylDecode 购物车功能将我的购物车数据存储到 Laravel 中的数据库

How can I store my Cart data to database in Laravel using DarrylDecode Cart functionalities

我试图通过制作一个小型电子商务网站项目和实现购物车功能来学习Laravel,我遇到了 DarrylDecode 购物车功能 (https://github.com/darryldecode/laravelshoppingcart)

但我很快意识到用户的购物车数据存储在会话中,每当用户注销并再次登录时,购物车数据就会丢失。用户也无法从其他浏览器或其他设备访问购物车项目,因为它临时保存在特定浏览器的会话中。我想将相同的数据存储到数据库中并从那里访问它。关于将数据存储在数据库中的文档解释中几乎没有关于此的内容,但还不是很清楚。谁能告诉我如何实现这个

Darryldecode 购物车是一种在您的项目中实现购物车功能的双向方法。在我的例子中,我正在尝试对心愿单使用持久存储,这样当用户登录时,他们仍然会看到他们的心愿单项目。首先要做的是通过 运行 命令

创建一个迁移
php artisan make:migration create_wishlist_storage_table

这将在 database/migration 目录中创建迁移文件,打开文件,并用这些代码行替换整个代码块。

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWishlistStorageTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
{
    Schema::create('wishlist_storage', function (Blueprint $table) {
        $table->string('id')->index();
        $table->longText('wishlist_data');
        $table->timestamps();
        $table->primary('id');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('wishlist_storage');
}
}

之后,运行 php artisan migrate 命令。这将在您的数据库中创建一个 wishlist_storage table 列 id wishlist_data,timestamps.The 接下来是创建一个 eloquent 模型来处理我们的迁移通过 运行 命令 php artisan make:model DatabaseStorageModel。在你的应用程序目录中打开 DatabaseStorageModel.php 文件,并将整个代码块替换为以下代码行。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class DatabaseStorageModel extends Model
{
//
/**
 * Override eloquent default table
 * @var string
 */
protected $table = 'wishlist_storage';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'id', 'wishlist_data',
];


/**
 * Mutator for wishlist_column
 * @param $value
 */
public function setWishlistDataAttribute($value)
{
    $this->attributes['wishlist_data'] = serialize($value);
}


/**
 * Accessor for wishlist_column
 * @param $value
 * @return mixed
 */
public function getWishlistDataAttribute($value)
{
    return unserialize($value);
}

}

接下来要做的是创建一个新的 class 以注入到我们的购物车实例中。为此,使用您的应用命名空间创建一个名为 DatabaseStorage.php 的文件并粘贴这行代码。

<?php
namespace App;

use Darryldecode\Cart\CartCollection;

class DatabaseStorage {

public function has($key)
{
    return DatabaseStorageModel::find($key);
}


public function get($key)
{
    if($this->has($key))
    {
        return new CartCollection(DatabaseStorageModel::find($key)->wishlist_data);
    }
    else
    {
        return [];
    }
}


public function put($key, $value)
{
    if($row = DatabaseStorageModel::find($key))
    {
        // update
        $row->wishlist_data = $value;
        $row->save();
    }
    else
    {
        DatabaseStorageModel::create([
            'id' => $key,
            'wishlist_data' => $value
        ]);
    }
}


}

这取决于您命名文件的方式和 classes,但我正在解释我是如何命名的。最后一步是让 DatabaseStorage class 成为我们购物车的默认存储。 运行 命令

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

在config目录下发布库配置文件名shopping_cart.php。打开 shopping_cart.php 文件并替换

'storage'=>null,

'storage' => \App\DatabaseStorage::class,

您现在可以按照正常程序在您的控制器中使用手推车。

当我使用它时,我发现购物车现在是 public。 如果一个用户从购物车中完全删除该项目,而拥有相同项目的其他用户则看不到它。