Skip to content

Commit

Permalink
add 記事のモデル、マイグレーション、ファクトリを作る
Browse files Browse the repository at this point in the history
  • Loading branch information
ucan-lab committed Oct 17, 2018
1 parent cad45cb commit 1c17b14
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
/**
* Get the user
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
10 changes: 10 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ class User extends Authenticatable
protected $hidden = [
'password', 'remember_token',
];

/**
* Get the posts
*
* @return void
*/
public function posts()
{
return $this->hasMany(Post::class);
}
}
10 changes: 10 additions & 0 deletions database/factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Faker\Generator as Faker;

$factory->define(App\Post::class, function (Faker $faker) {
return [
'title' => $faker->sentence,
'content' => $faker->text,
];
});
34 changes: 34 additions & 0 deletions database/migrations/2018_10_17_061132_create_posts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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->unsignedInteger('user_id');
$table->string('title');
$table->text('content');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
5 changes: 4 additions & 1 deletion database/seeds/UsersTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Database\Seeder;
use App\User;
use App\Post;

class UsersTableSeeder extends Seeder
{
Expand All @@ -14,6 +15,8 @@ public function run()
{
User::truncate();

factory(User::class, 30)->create();
factory(User::class, 50)->create()->each(function ($user) {
factory(Post::class, 10)->create(['user_id' => $user->id]);
});
}
}

0 comments on commit 1c17b14

Please sign in to comment.