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

Appearance settings

Commit 79589be

Browse filesBrowse files
User, Posts & Comments Relations
1 parent 5098994 commit 79589be
Copy full SHA for 79589be

File tree

7 files changed

+20
-16
lines changed
Filter options

7 files changed

+20
-16
lines changed

‎app/Models/Comment.php

Copy file name to clipboardExpand all lines: app/Models/Comment.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@
66

77
class Comment extends Model
88
{
9-
//
9+
public function post()
10+
{
11+
return $this->belongsTo('App\Models\Post');
12+
}
1013
}

‎app/Models/Post.php

Copy file name to clipboardExpand all lines: app/Models/Post.php
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,13 @@
66

77
class Post extends Model
88
{
9-
//
9+
public function user()
10+
{
11+
return $this->belongsTo('App\Models\User');
12+
}
13+
14+
public function comments()
15+
{
16+
return $this->hasMany('App\Models\Comment');
17+
}
1018
}

‎app/Models/User.php

Copy file name to clipboardExpand all lines: app/Models/User.php
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ class User extends Authenticatable
3636
protected $casts = [
3737
'email_verified_at' => 'datetime',
3838
];
39+
40+
public function posts()
41+
{
42+
return $this->hasMany('App\Models\Post');
43+
}
3944
}

‎database/factories/CommentFactory.php

Copy file name to clipboardExpand all lines: database/factories/CommentFactory.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
/* @var $factory \Illuminate\Database\Eloquent\Factory */
44

5+
use App\Models\Post;
56
use App\Models\Comment;
67
use Faker\Generator as Faker;
7-
use App\Models\Post;
88

99
$factory->define(Comment::class, function (Faker $faker) {
1010
return [

‎database/factories/PostFactory.php

Copy file name to clipboardExpand all lines: database/factories/PostFactory.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
/* @var $factory \Illuminate\Database\Eloquent\Factory */
44

5+
use App\Models\User;
56
use App\Models\Post;
67
use Faker\Generator as Faker;
7-
use App\Models\User;
88

99
$factory->define(Post::class, function (Faker $faker) {
1010
return [

‎database/factories/UserFactory.php

Copy file name to clipboardExpand all lines: database/factories/UserFactory.php
-11Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@
55
use Illuminate\Support\Str;
66
use Faker\Generator as Faker;
77

8-
/*
9-
|--------------------------------------------------------------------------
10-
| Model Factories
11-
|--------------------------------------------------------------------------
12-
|
13-
| This directory should contain each of the model factory definitions for
14-
| your application. Factories provide a convenient way to generate new
15-
| model instances for testing / seeding your application's database.
16-
|
17-
*/
18-
198
$factory->define(User::class, function (Faker $faker) {
209
return [
2110
'name' => $faker->name,

‎database/seeds/DatabaseSeeder.php

Copy file name to clipboardExpand all lines: database/seeds/DatabaseSeeder.php
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class DatabaseSeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
// $this->call(UsersTableSeeder::class);
1514
factory(\App\Models\User::class, 10)->create();
1615
factory(\App\Models\Post::class, 50)->create();
1716
factory(\App\Models\Comment::class, 300)->create();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.