Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用DarryDecode购物车功能将购物车数据存储到Laravel中的数据库_Php_Laravel_Shopping Cart - Fatal编程技术网

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

Php 如何使用DarryDecode购物车功能将购物车数据存储到Laravel中的数据库,php,laravel,shopping-cart,Php,Laravel,Shopping Cart,我试图通过制作一个小型电子商务网站项目来学习Laravel,为了实现购物车功能,我遇到了DarrylDecode购物车功能() 但很快我意识到,用户的购物车数据存储在会话中,每当用户注销并再次登录时,购物车数据就会丢失。用户无法从其他浏览器或其他设备访问购物车项目,因为购物车项目临时保存在特定浏览器上的会话中。我想将相同的数据存储到数据库中,并从那里访问它。文档中很少有关于在数据库中存储数据的内容,但这并不清楚。有谁能告诉我如何实现这一点吗Darrydecode cart是在项目中实现cart功

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


但很快我意识到,用户的购物车数据存储在会话中,每当用户注销并再次登录时,购物车数据就会丢失。用户无法从其他浏览器或其他设备访问购物车项目,因为购物车项目临时保存在特定浏览器上的会话中。我想将相同的数据存储到数据库中,并从那里访问它。文档中很少有关于在数据库中存储数据的内容,但这并不清楚。有谁能告诉我如何实现这一点吗Darrydecode cart是在项目中实现cart功能的一种双向方法。在我的例子中,我试图为愿望列表使用持久存储,这样当用户登录时,他们仍然可以看到他们的愿望列表项。要做的第一件事是通过运行命令创建迁移

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

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);
}

}
<?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的文件,并粘贴这行代码

<?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

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);
}

}
<?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
        ]);
    }
}


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

'storage'=>null,


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

当我使用此工具时,我发现购物车现在对所有人公开。
如果一个用户删除了该项目,则该项目将完全从购物车中删除,而拥有相同项目的其他用户无法看到该项目。

您说它正在使用会话,对吗?您是否已尝试获取存储在会话中的购物车数据并将其保存在数据库中?这甚至与软件包的使用无关,而是与基本的Laravel有关。谢谢分享。老实说,您应该为非身份验证用户使用会话或cookie,为身份验证用户使用数据库。另外,不要忘记在每个身份验证用户之前启动新会话,否则您将为所有用户获得相同的购物车内容<代码>$wishlist\u cart=app()->make(WishlistCart::class);if(Auth::check()){**$wishlist_cart->session(Auth::user()->id);**}else{$wishlist_cart->setSession(app('session'));}