【问题标题】:i can't restore cart data from database with Crinsane/LaravelShoppingcart我无法使用 Crinsane/LaravelShoppingcart 从数据库中恢复购物车数据
【发布时间】:2017-12-05 09:11:48
【问题描述】:

我正在使用 Crinsane/LaravelShoppingcart 包,我在迁移它时在购物车表上进行了一些编辑并像这样添加

    Schema::create(config('cart.database.table'), function (Blueprint $table) {
        $table->string('identifier');
        $table->integer('userId')->unsigned();
        $table->text('address');
        $table->string('instance');
        $table->longText('content');
        // $table->nullableTimestamps();
        $table->timestamps();



        $table->primary(['identifier', 'instance']);
        $table->foreign('userId')->references('id')->on('users')->onDelete('cascade');
    });

并且还像这样在 Cart 类中编辑了 store 方法(添加了两个参数来存储 $userId 和 $address)

public function store($identifier, $address, $userId)
{
    $content = $this->getContent();

    if ($this->storedCartWithIdentifierExists($identifier)) {
        throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
    }

    $this->getConnection()->table($this->getTableName())->insert([
        'identifier' => $identifier,
        'address' => $address,
        'userId' => $userId,
        'instance' => $this->currentInstance(),
        'content' => serialize($content)
    ]);

    $this->events->fire('cart.stored');
}  

并且在数据库中一切正常,但问题是当我使用恢复方法从数据库恢复数据时,仅使用一个参数($identifier)恢复它什么也没给我,当我 dd(Cart::restore(Auth ::user()->id)) 它给了我“null”请帮我解决这个问题

【问题讨论】:

    标签: php database laravel e-commerce laravel-5.5


    【解决方案1】:

    您已将store 方法修改为接受$userId,但您说restore 方法不起作用。您还可以通过修改restore 以接受$userId 来解决此特定问题,因为您将用户ID 传递给它,但它会根据shoppingcart 数据库中的idenfitier 列检查它。

    但是,这仍然是一个坏主意。永远不要修改第三方包,因为它会被下一个 composer update 覆盖。

    更好的解决方案是简单地使用$identifer 来存储Auth::user()->id。如果您需要为每个用户存储多个购物车,您可以使用此软件包的愿望清单功能。作为最后的手段,您可以简单地将 id 与标识符连接起来,例如。 'firstcart'+userId'secondcart'+userId

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-16
      • 2017-11-05
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      • 2023-03-16
      • 2020-01-31
      相关资源
      最近更新 更多