YuHo のブログ

Laravel, PHP, JavaScript

Laravel 5.6 でブログサイトを制作する #1

仮想マシンの立ち上げとログイン

homestead up
homestead ssh

仮想マシンディレクトリ移動

cd code

プロジェクトの立ち上げ

laravel new project

仮想マシンディレクトリ移動

cd project

認証機能の準備

php artisan make:auth
php artisan migrate

Post モデルと Migration の作成

php artisan make:model Post -m

Post Migration の編集

<?php

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

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('title', 255);
            $table->string('content', 255);
            $table->timestamps();
        });
    }

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

Comment モデルと Migration の作成

php artisan make:model Comment -m

Comment Migration の編集

<?php

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

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('post_id');
            $table->string('content', 255);
            $table->timestamps();
        });
    }

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

Post Factory の作成

php artisan make:factory PostFactory --model=Post

Post Factory の編集

<?php

use Faker\Generator as Faker;

$factory->define(App\Post::class, function (Faker $faker) {
    return [
        'title' => $faker->realText($maxNbChars = 200, $indexSize = 2),
        'content' => $faker->realText($maxNbChars = 200, $indexSize = 2),
    ];
});

Seeder の編集

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\User::class, 30)->create()->each(function ($u) {
            $u->posts()->save(factory(App\Post::class)->make());
        });
    }
}

auto-loader の更新

composer dump-autoload

データベースを更新して Seed を実行する

php artisan migrate:refresh --seed

簡単な Pagination

<?php

Route::get('/', function () {
    $posts = App\Post::paginate(10);
    
    return view('welcome', ['posts' => $posts]);
});
    <body>
        <div class="flex-center position-ref full-height">
            @if (Route::has('login'))
                <div class="top-right links">
                    @auth
                        <a href="{{ url('/home') }}">Home</a>
                    @else
                        <a href="{{ route('login') }}">Login</a>
                        <a href="{{ route('register') }}">Register</a>
                    @endauth
                </div>
            @endif
            
            <div class="content">
                @foreach($posts as $post)
                    <p>{{ $post->title }}</p>
                @endforeach
            </div>

        </div>
    </body>