0
0

More than 5 years have passed since last update.

HerokuにHeroku Postgresを導入してアプリケーションを動かしてみる3

Posted at

DBに値が入らない

addページから値を入力するとテーブルへinsertされるはずがまたエラーが発生した。
スクリーンショット 2018-10-15 21.37.27.png

BoardControllerのクラスが存在しないとな。
ソースを見返して見たら単純にweb.phpのスペルミスだった。安心した。
これでデータの追加ができるようになった!

スクリーンショット 2018-10-15 21.51.43.png

次は本格的にCRUDを作ってみる。

CRUDを作る テーブル作成編

前回とboardsテーブルと同じやり方でpeopleテーブルを作成してみる。

command
php artisan make:migration create_people_table

マイグレーションファイルを作成する。
xxxx_create_people_tableを編集。

xxxx_create_people_table
<?php

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

class CreatePeopleTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('people', function (Blueprint $table){
            $table->increments('id');
            $table->string('name');
            $table->string('mail');
            $table->integer('age');
            $table->timestamps();
        });
    }

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

作成後、ローカル環境でdatabase.sqliteを削除し、touch database.sqliteで空ファイルを作成しマイグレーション。

command
php artisan migrate

シーダーファイルの作成

シードを作成するためのシーダーファイルを作成する。

command
php artisan make:seeder PeopleTableSeeder

成功すると「PeopleTableSeeder.php」というファイルが作成される。
作成されたファイルは下記のように追記する。

PeopleTableseseeder.php
<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class PeopleTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $param = [
            'name' => 'taro',
            'mail' => 'taro@yamada.jp',
            'age' => 12,
        ];
        DB::table('people')->insert($param);

        $param = [
            'name' => 'hanako',
            'mail' => 'hanako@flower.jp',
            'age' => 34,
        ];
        DB::table('people')->insert($param);

        $param = [
            'name' => 'sachiko',
            'mail' => 'sachiko@happy.jp',
            'age' => 56,
        ];
        DB::table('people')->insert($param);
    }
}

「DatabaseSeeder.php」は下記のように追記する。

DatabaseSeeder.php
<?php

use Illuminate\Database\Seeder;


class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        $this->call(PeopleTableSeeder::class);
    }
}

モデルの作成

Personモデルを作成する。

command
php artisan make:model Person

成功するとPerson.phpというファイルが作成される。

PersonControllerの作成

command
php artisan make:controller PersonController

成功するとPersonController.phpというファイルが作成される。
作成されたファイルに追記する。

PersonController.php
<?php

namespace App\Http\Controllers;

use App\Person;
use Illuminate\Http\Request;

class PersonController extends Controller
{
    public function index(Request $request)
    {
        $items = Person::all();
        return view('person.index', ['items' => $items]);
    }
}

index.blade.phpの作成

index.blade.php
@extends('layouts.helloapp')

@section('title', 'Person.index')

@section('menubar')
    @parent
    インデックスページ
@endsection

@section('content')
    <table>
        <tr><th>Name</th><th>Mail</th><th>Agr</th></tr>
        @foreach ($items as $item)
            <tr>
                <td>{{$item->name}}</td>
                <td>{{$item->mail}}</td>
                <td>{{$item->age}}</td>
            </tr>
        @endforeach
    </table>
@endsection

@section('footer')
copyright 2017 tanaka
@endsection

最後にweb.phpにルート情報を追記する。

web.php
Route::get('person', 'PersonController@index');

/pesronにアクセス正常ならOK!

スクリーンショット 2018-10-16 22.43.31.png

Personクラスを拡張してみる

Personクラスを以下に書き換える。

Person.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
    public function getData()
    {
        return $this->id . ': ' . $this->name . '(' . $this->age . ')';
    }
}

index.blade.phpも以下のように書き換える

index.blade.php
@extends('layouts.helloapp')

@section('title', 'Person.index')

@section('menubar')
    @parent
    インデックスページ
@endsection


@section('content')
    <table>
        <tr><th>Data</th></tr>
        @foreach ($items as $item)
            <tr>
                <td>{{$item->getData()}}</td>
            </tr>
        @endforeach
    </table>
@endsection

@section('footer')
copyright 2017 tanaka
@endsection

うまく修正できるとスクリーンショットのようにデータをまとめられる。

スクリーンショット 2018-10-16 22.56.20.png

IDによる検索

idからデータ検索できるようにする。
find.blade.phpから作成していく。

find.blade.php
@extends('layouts.helloapp')

@section('title', 'Person.find')

@section('menubar')
    @parent
    検索ページ
@endsection

@section('content')
    <form action="/person/find" method="post">
        {{ csrf_field() }}
        <input type="text" name="input" value="{{$input}}">
        <input type="submit" value="find">
    </form>
    @if (isset($item))
    <table>
        <tr><th>Data</th></tr>
        <tr>
            <td>{{$item->getData()}}</td>
        </tr>
    </table>
    @endif
@endsection

@section('footer')
copyright 2017 tanaka.
@endsection

PersonController に追加する。

PersonController.php
public function find(Request $request)
{
    return view('person.find', ['input' => '']);
}

public function search(Request $request)
{
    $item = Person::find($request->input);
    $param = ['input' => $request->input, 'item' => $item];
    return view('person.find', $param);
}

web.phpにルート情報も追加する。

web.php
Route::get('person/find', 'PersonController@find');
Route::post('person/find', 'PersonController@search');

修正完了し下記のように表示されればOK!

スクリーンショット 2018-10-16 23.22.48.png

今日はここまで明日はスコープについて勉強しよう。

0
0
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
0
0