【Laravel5.8】LaravelShoppingCartを使いたい

インストール

Laravel5.5までは、

$ composer require gloudemans/shoppingcart

github.com


でしたが、

Laravel5.8では、

$ composer require bumbummen99/shoppingcart

github.com

こちらを使います。

プロバイダー・エイリアスへの設定

config/app.php

//サービスプロバイダー
Gloudemans\Shoppingcart\ShoppingcartServiceProvider::class

//エイリアス
'Cart' => Gloudemans\Shoppingcart\Facades\Cart::class

configファイルをコピー

$ php artisan vendor:publish

config/cart.phpへの設定

// 消費税に合わせて設定
    'tax' => 8,
//日本円の場合は以下のように設定
    'format' => [
        'decimals' => 0,
        'decimal_point' => '',
        'thousand_seperator' => ','
    ],

Controller

    public function bookToCart($book_id) {
        $book = Book::findOrFail($book_id);
        
        Cart::add([
            [
                'id' => $book->id,
                'name' => $book->title,
                'qty' => '1',
                'price' => $book->price,
                'weight' => '1',
                'options' => ['photo_path'=> $book->photo_path]
                ]
            ]);
        
        $carts = Cart::content();
        return view('customer.cart')->with(compact('carts'));
    }

LaravelShoppingCartには、qty(量)とweight(重さ)が初期値で設定されていました。
今回作成しているアプリの場合、量も重さも必要ありませんが、設定の解き方が分からなかったので、やや強引ですが「1」を代入。

またCart::addの配列に入れるkeyは、

  1. id
  2. name
  3. qty
  4. price
  5. weight
  6. options

の順番で決まっているので注意!

view

        <table class="uk-table uk-table-hover uk-table-divider">
            <thead>
                <th>購入商品</th>
                <th></th>
                <th>小計</th>
            </thead>
            <tbody>
                <?php $total = 0 ?>
                @foreach($carts as $cart)
                <tr>
                    <td><img src="{{$cart->options->photo_path}}" width="50" height="50"></td>
                    <td>{{$cart->name}}</td>
                    <td>{{$cart->price}}円</td>
                    <td><button class="uk-button uk-button-danger uk-button">削除</button></td>
                </tr>
                <?php $total +=  $cart->price ?>
                @endforeach
                <tr>
                    <td></td>
                    <td class="uk-text-large" style="text-align:right;">合計</td>
                    <td class="uk-text-large">{{$total}}円</td>

カート内全削除・カート内アイテム削除

        <table class="uk-table uk-table-hover uk-table-divider">
            <thead>
                <th>購入商品</th>
                <th></th>
                <th>小計</th>
            </thead>
            <tbody>
                <?php $total = 0 ?>
                @foreach($carts as $cart)
                <tr>
                    <td><img src="{{$cart->options->photo_path}}" width="50" height="50"></td>
                    <td>{{$cart->name}}</td>
                    <td>{{$cart->price}}円</td>
                    <td><a href="/cart/remove/{{$cart->rowId}}"><button class="uk-button uk-button-danger uk-button">削除</button></a></td>
                </tr> ★
                <?php $total +=  $cart->price ?>
                @endforeach
                <tr>
                    <td></td>
                    <td class="uk-text-large" style="text-align:right;">合計(消費税込み)</td>
                    <td class="uk-text-large">{{$total * 1.08}}円</td>
                    <td>
                        <form action="/paymentComplete">
                                <script
                                    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                                    data-key="{{ env('STRIPE_PUBLIC_KEY') }}"
                                    data-amount={{$total * 1.08}}
                                    data-name="古本屋さん"
                                    data-label="決済をする"
                                    data-description="Online shopping by Stripe"
                                    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                                    data-locale="auto"
                                    data-currency="JPY">
                                </script>
                        </form>
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td><td><a href="/cart/reset"><button class="uk-button uk-button-danger uk-button">カート全削除</button></a></td></td> ★
                </tr>
            </tbody>
        </table>
//カート全削除
Route::get('/cart/reset', 'CustomerController@reset');

//アイテム1つ削除
Route::get('/cart/remove/{rowId}', 'CustomerController@remove');
    public function reset() {
        Cart::destroy();
        return redirect('/cart');
    }

    public function remove($rowId) {
        Cart::remove($rowId);
        return redirect('/cart');
    }

全削除は「Cart::destroy();」でOKなのですが、
アイテム1つの削除は、「$rowId」というカラムを引数にして削除する必要があります。

カートにアイテムを追加すると勝手に「$rowId」が追加されるため、普通のカラムを引き出すように「$carts->$rowId」と引っ張ってくればOKです。

ルーティング

Route::get('/bookToCart/{book_id}', 'CustomerController@bookToCart');