質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

1回答

2639閲覧

laravelを用いてECサイトのカート機能を実装してDBの個数を減らしたい。

shinichi_mori

総合スコア0

Laravel

LaravelとはTaylor Otwellによって開発された、オープンソースなPHPフレームワークです。Laravelはシンプルで表現的なシンタックスを持ち合わせており、ウェブアプリケーション開発の手助けをしてくれます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2020/11/10 10:35

編集2020/11/13 15:55

前提・実現したいこと

PHP(Laravel)でECサイトを制作しております。
現在実装している内容は以下です。

  • 商品一覧ページ
  • カート機能
  • 購入機能
  • 自動メール送信機能
  • 決済機能

今回実現させたい事として商品一覧ページで数量指定した後にカートページで指定された個数を表示させて決済する。
そこから商品テーブルから購入された分の個数を減らしていくコードを実装したいと思っています。

発生している問題・エラーメッセージ

特にエラーが出ているわけではないのですが、どのように実装すればいいのか…皆目検討がついていない状態です…

該当のソースコード

public function showCart() { $user_id = Auth::id(); $data['my_carts'] = $this->where('user_id', $user_id)->get(); // 合計金額のと個数を表示 $data['count'] = 0; $data['sum'] = 0; foreach($data['my_carts'] as $my_cart){ $data['count'] ++; $data['sum'] += $my_cart->item->price; $data['sell_count'] = $this->where('item_id', $my_cart->item0>id)->count(); } return $data; }

###マイグレーションファイル

items

1use Illuminate\Database\Migrations\Migration; 2use Illuminate\Database\Schema\Blueprint; 3use Illuminate\Support\Facades\Schema; 4 5class CreateItemsTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::create('items', function (Blueprint $table) { 15 $table->bigIncrements('id'); 16 $table->string('name', 50)->nullable(false); 17 $table->integer('price')->nullable(false); 18 $table->text('introduction')->nullable(false); 19 $table->integer('stock_amount')->nullable(false); 20 $table->integer('buy_amount')->nullable(false); 21 $table->timestamps(); 22 }); 23 } 24 25 /** 26 * Reverse the migrations. 27 * 28 * @return void 29 */ 30 public function down() 31 { 32 Schema::dropIfExists('items'); 33 }

carts

1use Illuminate\Database\Migrations\Migration; 2use Illuminate\Database\Schema\Blueprint; 3use Illuminate\Support\Facades\Schema; 4 5class CreateCartsTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::create('carts', function (Blueprint $table) { 15 $table->bigIncrements('id'); 16 $table->integer('item_id'); 17 $table->integer('user_id'); 18 $table->timestamps(); 19 }); 20 } 21 22 /** 23 * Reverse the migrations. 24 * 25 * @return void 26 */ 27 public function down() 28 { 29 Schema::dropIfExists('carts'); 30 }

items追加分

1use Illuminate\Database\Migrations\Migration; 2use Illuminate\Database\Schema\Blueprint; 3use Illuminate\Support\Facades\Schema; 4 5class AddImgpathToItemsTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::table('items', function (Blueprint $table) { 15 // 16 $table->string('imgpath', '200')->nullable(false); 17 }); 18 } 19 20 /** 21 * Reverse the migrations. 22 * 23 * @return void 24 */ 25 public function down() 26 { 27 Schema::table('items', function (Blueprint $table) { 28 // 29 }); 30 }

items追加分

1use Illuminate\Database\Migrations\Migration; 2use Illuminate\Database\Schema\Blueprint; 3use Illuminate\Support\Facades\Schema; 4 5class AddStockToItemsTable extends Migration 6{ 7 /** 8 * Run the migrations. 9 * 10 * @return void 11 */ 12 public function up() 13 { 14 Schema::table('items', function (Blueprint $table) { 15 // 16 $table->integer('stock')->nullable(false); 17 }); 18 } 19 20 /** 21 * Reverse the migrations. 22 * 23 * @return void 24 */ 25 public function down() 26 { 27 Schema::table('items', function (Blueprint $table) { 28 // 29 }); 30 }

試したこと

where句を用いて情報を取得して、それをcount関数で数えてあげればいいのかなと単純な考えで思考をしてみました。

補足情報(FW/ツールのバージョンなど)

PhP 7.3.11

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kai0310

2020/11/10 12:25

DB構造がわかる様にマイグレーションファイルを提示してください。
shinichi_mori

2020/11/11 03:04

kai0310さん、返答いただきありがとうございます。 情報が足りておらず申し訳ございません。 以下マイグレーションファイルになります。 こちらで問題ありませんでしょうか?? ```items use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateItemsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('items', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name', 50)->nullable(false); $table->integer('price')->nullable(false); $table->text('introduction')->nullable(false); $table->integer('stock_amount')->nullable(false); $table->integer('buy_amount')->nullable(false); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('items'); } ``` ```carts use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateCartsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('carts', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('item_id'); $table->integer('user_id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('carts'); } ``` ```追加分 use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddImgpathToItemsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('items', function (Blueprint $table) { // $table->string('imgpath', '200')->nullable(false); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('items', function (Blueprint $table) { // }); } ``` ```追加分 use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class AddStockToItemsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::table('items', function (Blueprint $table) { // $table->integer('stock')->nullable(false); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::table('items', function (Blueprint $table) { // }); } ```
kai0310

2020/11/11 11:07

質問本文は修正できますので、そちらに記述してください。
shinichi_mori

2020/11/13 15:56

ご返答が遅くなり大変申し訳ございません。 本文の方に記述さていただきました! 何卒よろしくお願いいたします。
guest

回答1

0

商品テーブル(items)とカートテーブル(carts)に数量を保存しておくカラムを作成すれば簡単かと思います。

例えば、商品をカートに入れる時は数量をformから取得できるので、取得した値をそのままcartsテーブルに保存。

cartsテーブルより商品が購入された時は、cartsテーブルのレコードは削除、itemsテーブルの数量カラムから購入された分を引く、という処理にすれば実現できるかと思います。

投稿2020/11/17 05:06

howaito

総合スコア9

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問