Help us understand the problem. What is going on with this article?

Laravel データベース連携_データベース作成からデータベース参照してWeb表示まで

More than 1 year has passed since last update.

①studentsというデータベースを作成
②データベースにダミーデータ投入
③studentsのデータベース情報をwebに表示する

命名規則

モデル ⇒ Student.php
コントローラー ⇒ StudentsController.php
ビューのフォルダ ⇒ student

モデルの作成

artisan コマンド実行

php artisan make:model Student

カラムの宣言

# vi app/Student.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    //
        public $timestamps = false; //timesatampを利用しない
        protected $fillable = ['name', 'email','tel'];
}

仮データ投入

シーダ機能を用いる

php artisan make:seed StudentsTableSeeder

シーダファイルの編集

ダミーの名前を付与する。(便利だわ)

vi database/seeds/StudentsTableSeeder.php

vi database/seeds/StudentsTableSeeder.php


<?php

use Illuminate\Database\Seeder;

class StudentsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('students')->delete();
        $faker = Faker\Factory::create('ja_JP');
        for ($i = 0; $i < 10; $i++) {
                \App\Student::create([
                        'name'=> $faker->name(),
                        'email'=> $faker->email(),
                        'tel'=> $faker->phoneNumber()
                ]);
        }
    }
}

データベースにダミーデータを投入

php artisan db:seed --class=StudentsTableSeeder

ルーティング設定

vi router/web.php

Route::get('/student/list','StudentsController@getIndex');

コントローラの作成

artisanコマンドでコントローラのスケルトンを作成。

php artisan make:controller StudentsController

コントローラで表示先を指定する。(student.list)

vi app/Http/Controllers/StudentsController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class StudentsController extends Controller
{
    //
  public function getIndex()
  {
  $query = \App\Student::query();
  // 全件取得 +ページネーション
  $students = $query->orderBy('id','desc')->paginate(10);
  return view('student.list')->with('students',$students);
  }
}

Viewを作成

Bladeを利用して作成します。

親テーマ作成

三種類のファイルを作成します。

mkdir resources/views/layouts/partials

① vi resources/views/layouts/partials/header.blade.php

ources/views/layouts/partials/header.blade.php!-- header & grobal navi -->
<nav class="navbar navbar-default" style="background-color: #FFFFFF;">
  <div class="container-fluid">
  <div class="navbar-header">
  <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbarEexample2">
  <span class="sr-only">Toggle navigation</span>
  <span class="icon-bar"></span>
  <span class="icon-bar"></span>
  <span class="icon-bar"></span>
  </button>
  <a class="navbar-brand" href="#">
  <img alt="Laravel学習帳" src="/img/logo.png" style="height: 20px;">
  </a>
  </div>

  <div class="collapse navbar-collapse" id="navbarEexample2">
  <ul class="nav navbar-nav">
  <li class="active"><a href="#">menuA</a></li>
  <li><a href="#">menuB</a></li>
  <li><a href="#">menuC</a></li>
  </ul>
  </div>
  </div>
</nav>

② vi resources/views/layouts/partials/sidebar.blade.php

<!-- side bar -->
<div class="panel panel-default">
  <div class="panel-heading">
  Menu
  </div>
  <!-- <div class="panel-body"> -->
  <ul class="nav nav-pills nav-stacked">
  <li><a href=""><i class="glyphicon glyphicon-menu-right"></i> submenu1</a></li>
  <li><a href=""><i class="glyphicon glyphicon-menu-right"></i> submenu2</a></li>
  <li><a href=""><i class="glyphicon glyphicon-menu-right"></i> submenu3</a></li>
  <li><a href=""><i class="glyphicon glyphicon-menu-right"></i> submenu4</a></li>
  </ul>
  <!-- </div> -->
</div>

③vi resources/views/layouts/partials/footer.blade.php

<!-- footer -->
<footer class="footer">
  <div class="container">
  <p class="text-muted">Place sticky footer content here.</p>
  </div>
</footer>
~

Student表示用の子テーマを作成

mkdir resources/views/student/
vi resources/views/student/list.blade.php

@extends('layouts.layout')
@section('title', 'Tutrial for beginner')
@section('content')
 <div class="page-header" style="margin-top:-30px;padding-bottom:0px;">
  <h1><small>受講生一覧</small></h1>
  </div>
  <table class="table table-striped table-hover">
  <thead>
  <tr>
  <th>No</th>
  <th>name</th>
  <th>email</th>
  <th>tel</th>
  <th>opration</th>
  </tr>
  </thead>
  <tbody>
  @foreach($students as $student)
  <tr>
  <td>{{$student->id}}</td><td>{{$student->name}}</td><td>{{$student->email}}</td><td>{{$student->tel}}</td>
  <td>
  <a href="" class="btn btn-primary btn-sm">詳細</a>
  <a href="" class="btn btn-primary btn-sm">編集</a>
  <a href="" class="btn btn-danger btn-sm">削除</a>
  </td>
  </tr>
  @endforeach
  </tbody>
  </table>

 <!-- page control -->
 {!! $students->render() !!}

@endsection
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away