8000 Add models, migrations and database seeder · laravel-json-api/tutorial-app@271447e · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

< 8000 /div>
Appearance settings

Commit 271447e

Browse files
committed
Add models, migrations and database seeder
1 parent e6d4ee9 commit 271447e

File tree

8 files changed

+272
-1
lines changed

8 files changed

+272
-1
lines changed

app/Models/Comment.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
9+
class Comment extends Model
10+
{
11+
use HasFactory;
12+
13+
/**
14+
* @var string[]
15+
*/
16+
protected $fillable = ['content'];
17+
18+
/**
19+
* @return BelongsTo
20+
*/
21+
public function post(): BelongsTo
22+
{
23+
return $this->belongsTo(Post::class);
24+
}
25+
26+
/**
27+
* @return BelongsTo
28+
*/
29+
public function user(): BelongsTo
30+
{
31+
return $this->belongsTo(User::class);
32+
}
33+
}

app/Models/Post.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9+
use Illuminate\Database\Eloquent\Relations\HasMany;
10+
11+
class Post extends Model
12+
{
13+
use HasFactory;
14+
15+
/**
16+
* @var string[]
17+
*/
18+
protected $fillable = ['content', 'published_at', 'slug', 'title'];
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $casts = [
24+
'published_at' => 'datetime',
25+
];
26+
27+
/**
28+
* @return BelongsTo
29+
*/
30+
public function author(): BelongsTo
31+
{
32+
return $this->belongsTo(User::class);
33+
}
34+
35+
/**
36+
* @return HasMany
37+
*/
38+
public function comments(): HasMany
39+
{
40+
return $this->hasMany(Comment::class);
41+
}
42+
43+
/**
44+
* @return BelongsToMany
45+
*/
46+
public function tags(): BelongsToMany
47+
{
48+
return $this->belongsToMany(Tag::class);
49+
}
50+
}

app/Models/Tag.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8+
9+
class Tag extends Model
10+
{
11+
use HasFactory;
12+
13+
/**
14+
* @var string[]
15+
*/
16+
protected $fillable = ['name'];
17+
18+
/**
19+
* @return BelongsToMany
20+
*/
21+
public function posts(): BelongsToMany
22+
{
23+
return $this->belongsToMany(Post::class);
24+
}
25+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreatePostsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('posts', function (Blueprint $table) {
17+
$table->id();
18+
$table->timestamps();
19+
$table->foreignId('author_id')->constrained('users')->cascadeOnDelete()->cascadeOnUpdate();
20+
$table->timestamp('published_at')->nullable();
21+
$table->string('title');
22+
$table->string('slug')->unique();
23+
$table->text('content');
24+
});
25+
}
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::dropIfExists('posts');
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateTagsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('tags', function (Blueprint $table) {
17+
$table->id();
18+
$table->timestamps();
19+
$table->string('name');
20+
});
21+
22+
Schema::create('post_tag', function (Blueprint $table) {
23+
$table->foreignId('post_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
24+
$table->foreignId('tag_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
25+
$table->primary(['post_id', 'tag_id']);
26+
});
27+
}
28+
29+
/**
30+
* Reverse the migrations.
31+
*
32+
* @return void
33+
*/
34+
public function down()
35+
{
36+
Schema::dropIfExists('post_tag');
37+
Schema::dropIfExists('tags');
38+
}
39+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateCommentsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('comments', function (Blueprint $table) {
17+
$table->id();
18+
$table->timestamps();
19+
$table->foreignId('post_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
20+
$table->foreignId('user_id')->constrained()->cascadeOnDelete()->cascadeOnUpdate();
21+
$table->text('content');
22+
});
23+
}
24+
25+
/**
26+
* Reverse the migrations.
27+
*
28+
* @return void
29+
*/
30+
public function down()
31+
{
32+
Schema::dropIfExists('comments');
33+
}
34+
}

database/seeders/DatabaseSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ class DatabaseSeeder extends Seeder
1313
*/
1414
public function run()
1515
{
16-
// \App\Models\User::factory(10)->create();
16+
$this->call(PostSeeder::class);
1717
}
1818
}

database/seeders/PostSeeder.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Database\Seeders;
4+
5+
use App\Models\Comment;
6+
use App\Models\Post;
7+
use App\Models\Tag;
8+
use App\Models\User;
9+
use Illuminate\Database\Seeder;
10+
11+
class PostSeeder extends Seeder
12+
{
13+
/**
14+
* Run the database seeds.
15+
*
16+
* @return void
17+
*/
18+
public function run()
19+
{
20+
$author = User::create([
21+
'name' => 'Artie Shaw',
22+
'email' => 'artie.shaw@jsonapi-tutorial.test',
23+
'password' => bcrypt('password'),
24+
'email_verified_at' => now(),
25+
]);
26+
27+
$commenter = User::create([
28+
'name' => 'Benny Goodman',
29+
'email' => 'benny.goodman@jsonapi-tutorial.test',
30+
'password' => bcrypt('password'),
31+
'email_verified_at' => now(),
32+
]);
33+
34+
$tag1 = Tag::create(['name' => 'Laravel']);
35+
$tag2 = Tag::create(['name' => 'JSON:API']);
36+
37+
$post = new Post([
38+
'title' => 'Welcome to Laravel JSON:API',
39+
'published_at' => now(),
40+
'content' => 'In our first blog post, you will learn all about Laravel JSON:API...',
41+
'slug' => 'welcome-to-laravel-jsonapi',
42+
]);
43+
44+
$post->author()->associate($author)->save();
45+
$post->tags()->saveMany([$tag1, $tag2]);
46+
47+
$comment = new Comment([
48+
'content' => 'Wow! Great first blog article. Looking forward to more!',
49+
]);
50+
51+
$comment->post()->associate($post);
52+
$comment->user()->associate($commenter)->save();
53+
}
54+
}

0 commit comments

Comments
 (0)
0