如何使用DarrylDecode Cart功能将我的Cart数据存储到Laravel中的数据库 [英] How can I store my Cart data to database in Laravel using DarrylDecode Cart functionalities

查看:89
本文介绍了如何使用DarrylDecode Cart功能将我的Cart数据存储到Laravel中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过制作一个小型的电子商务网站项目来学习Laravel,并且为了实现购物车功能,我遇到了DarrylDecode购物车功能(

I was trying to learn Laravel by making a small ecommerce website project and for implementing cart functionality, I came across DarrylDecode Cart functionality (https://github.com/darryldecode/laravelshoppingcart)

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

But soon I realised that the cart data for a user gets stored in a session and whenever the user logs out and log back in again, the cart data gets lost. The user cannot access the cart items from another browser or another device aswell since it is saved on a session on particular browser on temporary basis. I wanted to store the same data into database and access it from there. There are few things about that in the documentation explaing about storing data in database but that is not that clear. Can anyone give me an idea on how to achieve this

推荐答案

Darryldecode cart是一种在项目中实现购物车功能的双向方法.就我而言,我正在尝试对愿望清单使用持久性存储,以便用户登录时仍能看到其愿望清单项目.首先要做的是通过运行命令来创建迁移

Darryldecode cart is a two-way method for implementing cart functionality in your project. In my case, I'm trying to use persistent storage for the wishlist so that when users log in, they will still see their wishlist items. The first thing to do is to create a migration by running the command

php artisan make:migration create_wishlist_storage_table

这将在数据库/迁移目录中创建迁移文件,打开文件,然后用这些代码行替换整个代码块.

This will create the migration file in the database/migration directory, open the file, and replace the entire code block with these lines of code.

<?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 命令.这将在数​​据库中创建一个带有列表ID,wishlist_data和时间戳记的wishlist_storage表.下一步是通过运行命令 php artisan make:model DatabaseStorageModel 创建一个雄辩的模型来处理我们的迁移.打开您应用目录中的DatabaseStorageModel.php文件,并用以下代码行替换整个代码块.

After that, run the php artisan migrate command. This will create a wishlist_storage table in your database with columns id, wishlist_data, and the timestamps.The next thing is to create an eloquent model to handle our migration by running the command php artisan make:model DatabaseStorageModel. Open the DatabaseStorageModel.php file in your app directory and replace the entire code block with the following lines of code.

<?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的文件,然后粘贴以下代码行.

The next thing todo is to create a new class to be injected to our cart instance. For this, create a file named DatabaseStorage.php with your app namespace and paste this lines of code.

<?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类成为购物车的默认存储.运行命令

It's up to you the way you name your files and classes, but i'm explaining exactly how i did it. The final step is to make the DatabaseStorage class the default storage for our Cart. Run the command

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

在配置目录中发布库配置文件名称shopping_cart.php.打开shopping_cart.php文件并替换

to publish the library configuration file name shopping_cart.php in the config directory. Open the shopping_cart.php file and replace

'storage'=>null,

使用

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

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

You can now follow the normal procedure to use the cart in your controller.

这篇关于如何使用DarrylDecode Cart功能将我的Cart数据存储到Laravel中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆