10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MVCでカート機能2 - View編 for Laravel

Last updated at Posted at 2019-12-06

####考え方
Modelは単体データの操作を。
Viewは表示処理。
ControllerはViewのデータ処理。

####データベース
DBカラム
items : id | name | content | price | quantity | created_at | updated_at | deleted_at
carts : id | user_id | item_id | quantity | created_at | updated_at | deleted_at

####ソース

resources/view/cart/index.blade.php
@extends('layouts.mini_app')
@section('content')
<body>
@if (0 < $carts->count())
        <table>
                <h1>カート内容</h1>
                <tr style="background-color:#e3f0fb">
                        <th>商品名</th>
                        <th>購入数</th>
                        <th>価格</th>
                        <th>削除</th>
                </tr>
                @foreach ($carts as $cart)
                        <tr style="background-color:#f5f5f5">
                        <td align="right">{{ $cart->item->name }}</td>
                        <td align="right">{{ $cart->quantity }}</td>
                        <td align="right">{{ $cart->subtotal() }}</td>
                        <td><form method="post" action="{{ route('cart.delete') }}">
                                {{ csrf_field() }}
                                <input type="hidden" name="cart_id" value="{{ $cart->id }}">
                                <button type="submit">削除</button>
                        </form></td></tr>
                @endforeach
                <td style="background-color:#f5f5f5">
                        <td>合計</td>
                        <td>{{ $subtotals }}</td>
                        <td>税込: {{ $totals }}</td>
                        <td></td>
                </td>
        </table>
@else
        <h1>カートに商品はありません</h1>
@endif
<br>
<h2><a href="{{ route('item.index') }}">商品一覧へ戻る</a></h2>
</body>
@endsection
```

####解説
```php:コントローラーから受信した変数
$carts //collection-> model->cart
$subtotals //int 税抜合計
$totals //int 税込合計
```
LGTMお願いします
励みになります
10
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?