8
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.

laravelの学習のためにECサイトを作成した

Last updated at Posted at 2020-03-15

#データベース

create_stocks_table.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateStocksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('stocks', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
            $table->string('name','100');
            $table->string('detail','500');
            $table->integer('price');
            $table->string('imgpath','200');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('stocks');
    }
}
create_carts_table.php
<?php

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->bigInteger('stock_id');
           $table->bigInteger('user_id');
           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists('carts');
   }
}
create_users_table.php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

#トップページ
FireShot Capture 009 - LaraEC - localhost.png
トップページは商品一覧です。
sheederでデータベースに商品データを入力しています。

StockTableSeeder.php
class StockTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('stocks')->truncate();
        DB::table('stocks')->insert([
            'name' => 'マカロン',
            'detail' => '厳選した素材で作ったマカロンです。',
            'price' => 2000,
            'imgpath' => 'makaron.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'Bluetoothヘッドフォン',
            'detail' => 'ノイズキャンセリング機能搭載です。',
            'price' => 10000,
            'imgpath' => 'headphone.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => '目覚まし時計',
            'detail' => '電波時計、オートスヌーズ機能搭載です。',
            'price' => 2000,
            'imgpath' => 'clock.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'ドーナツ',
            'detail' => 'カロリーオフの美味しいドーナツです。',
            'price' => 120000,
            'imgpath' => 'donut.jpg',
        ]);
 
 
        DB::table('stocks')->insert([
            'name' => '高級腕時計',
            'detail' => 'メンズ用の高級腕時計です。',
            'price' => 198000,
            'imgpath' => 'watch.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'カメラレンズ35mm',
            'detail' => '最新式のカメラレンズです。',
            'price' => 79800,
            'imgpath' => 'lens.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'シャンパン',
            'detail' => '香りと味わいのバランスが良いシャンパンです。',
            'price' => 800,
            'imgpath' => 'shanpan.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'ビール',
            'detail' => '飲みやすいビールです。',
            'price' => 200,
            'imgpath' => 'beer.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => '食パン',
            'detail' => '素材にこだわった美味しい食パンです。',
            'price' => 500,
            'imgpath' => 'pan.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'スニーカー',
            'detail' => '軽くて履きやすいスニーカーです。',
            'price' => 3200,
            'imgpath' => 'sneaker.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'デスクトップパソコン',
            'detail' => '最新OS搭載のデスクトップパソコンです。',
            'price' => 11200,
            'imgpath' => 'pc.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'アコースティックギター',
            'detail' => '初心者向けのエントリーモデルです。',
            'price' => 25600,
            'imgpath' => 'aguiter.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'エレキギター',
            'detail' => '初心者向けのエントリーモデルです。',
            'price' => 15600,
            'imgpath' => 'eguiter.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => '麦わら帽子',
            'detail' => '夏にぴったりな麦わら帽子です。',
            'price' => 3200,
            'imgpath' => 'hat.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'メガネ',
            'detail' => 'シンプルな黒縁メガネです。',
            'price' => 4200,
            'imgpath' => 'megane.jpg',
        ]);
 
        DB::table('stocks')->insert([
            'name' => 'ロボット掃除機',
            'detail' => '自動で掃除してくれる便利な掃除機です。',
            'price' => 84200,
            'imgpath' => 'soujiki.jpg',
        ]);
    }
}

ページネーションはlaravelのlinksメソッドを使用しています。

shop.blade.php
@extends('layouts.app') @section('content')
<div class="contentsArea">
  <div id="link01" class="carousel slide mainBunner" data-ride="carousel">
    <div class="carousel-inner">
      <div class="carousel-item active">
        <img src="{{ asset('/image/bunner_01.jpg') }}" alt="">
      </div>
      <div class="carousel-item">
        <img src="{{ asset('/image/bunner_02.jpg') }}" alt="">
      </div>
      <a class="carousel-control-prev" href="#link01" role="button" data-slide="prev">
         <span class="carousel-control-prev-icon" aria-hidden="true"></span>
         <span class="sr-only">Previous</span>
     </a>
      <a class="carousel-control-next" href="#link01" role="button" data-slide="next">
         <span class="carousel-control-next-icon" aria-hidden="true"></span>
         <span class="sr-only">Next</span>
      </a>
    </div>
  </div>
  <div class="">
    <div class="block01">
      @foreach($stocks as $stock)
      <div class="block01_item">
        <img src="{{ asset('/image/'.$stock->imgpath) }}" alt="" class="block01_img">
        <p>{{$stock->name}}</p>
        <p class="c-red">{{number_format($stock->price)}}円</p>
        <p class="mb20">{{$stock->detail}} </p>
        <form action="{{ route('shop.cart') }}" method="post">
          @csrf
          <input type="hidden" name="stock_id" value="{{ $stock->id }}">
          <input type="submit" value="カートに入れる" class="btn-01">
        </form>
      </div>
      @endforeach
    </div>
    <div class="mt40">
      {{ $stocks->links() }}
    </div>
  </div>
</div>
@endsection

#カートページ
FireShot Capture 011 - LaraEC - localhost.png
カートに入れた商品の個数と合計金額が表示されるようになっています。

#購入完了ページ
FireShot Capture 012 - LaraEC - localhost.png
「購入する」ボタンを押すと確認ページに移動して、カートの中身がすべて削除されます。

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