【问题标题】:How can I store my Cart data to database in Laravel using DarrylDecode Cart functionalities如何使用 DarrylDecode 购物车功能将我的购物车数据存储到 Laravel 中的数据库
【发布时间】:2020-08-02 04:28:51
【问题描述】:

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

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

【问题讨论】:

  • 你说它正在使用会话,对吧?您是否已经尝试获取存储在会话中的购物车数据并将其保存在数据库中?这甚至与包的使用无关,而是与基本的 Laravel 相关。

标签: php laravel shopping-cart


【解决方案1】:

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 表,其中包含列id、wishlist_data 和时间戳。接下来是通过运行命令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);
}

}

接下来要做的是创建一个新类以注入到我们的购物车实例中。为此,使用您的应用命名空间创建一个名为 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
        ]);
    }
}


}

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

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

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

'storage'=>null,

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

您现在可以按照正常程序在控制器中使用购物车。

【讨论】:

  • 感谢分享。老实说,您应该为非身份验证用户使用会话或 cookie,为身份验证用户使用数据库。也不要忘记在每个 auth 用户之前启动新会话,否则您将获得所有用户相同的购物车内容。 $wishlist_cart = app()-&gt;make(WishlistCart::class); if (Auth::check()) { **$wishlist_cart-&gt;session(Auth::user()-&gt;id);** } else { $wishlist_cart-&gt;setSession(app('session')); }
  • 这是转到数据库表还是只是一个会话?
【解决方案2】:

当我使用它时,我发现购物车现在对所有人都是公开的。 如果一个用户删除了该商品,则该商品将完全从购物车中删除,而拥有相同商品的其他用户则看不到这一点。

【讨论】:

猜你喜欢
  • 2013-02-05
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-26
  • 1970-01-01
相关资源
最近更新 更多