DBに値が入らない
addページから値を入力するとテーブルへinsertされるはずがまたエラーが発生した。
BoardControllerのクラスが存在しないとな。
ソースを見返して見たら単純にweb.phpのスペルミスだった。安心した。
これでデータの追加ができるようになった!
次は本格的にCRUDを作ってみる。
CRUDを作る テーブル作成編
前回とboardsテーブルと同じやり方でpeopleテーブルを作成してみる。
php artisan make:migration 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で空ファイルを作成しマイグレーション。
php artisan migrate
シーダーファイルの作成
シードを作成するためのシーダーファイルを作成する。
php artisan make:seeder PeopleTableSeeder
成功すると「PeopleTableSeeder.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」は下記のように追記する。
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(PeopleTableSeeder::class);
}
}
モデルの作成
Personモデルを作成する。
php artisan make:model Person
成功するとPerson.phpというファイルが作成される。
PersonControllerの作成
php artisan make:controller PersonController
成功すると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の作成
@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にルート情報を追記する。
Route::get('person', 'PersonController@index');
/pesronにアクセス正常ならOK!
Personクラスを拡張してみる
Personクラスを以下に書き換える。
<?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も以下のように書き換える
@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
うまく修正できるとスクリーンショットのようにデータをまとめられる。
IDによる検索
idからデータ検索できるようにする。
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 に追加する。
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にルート情報も追加する。
Route::get('person/find', 'PersonController@find');
Route::post('person/find', 'PersonController@search');
修正完了し下記のように表示されればOK!
今日はここまで明日はスコープについて勉強しよう。