Skip to content

Commit e89940e

Browse files
committed
Edit like seeder logic
1 parent 1bf7f2b commit e89940e

File tree

2 files changed

+35
-44
lines changed

2 files changed

+35
-44
lines changed

backend/database/factories/LikeFactory.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

backend/database/seeds/LikeTableSeeder.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
use App\Entity\Like;
55
use App\Entity\Tweet;
66
use App\Entity\User;
7+
use Carbon\Carbon;
78
use Illuminate\Database\Seeder;
89
use Illuminate\Support\Facades\DB;
910

1011
class LikeTableSeeder extends Seeder
1112
{
12-
private const MIN_LIKES_COUNT = 1;
13-
private const MAX_LIKES_COUNT = 5;
13+
private const LIKES_COUNT = 10;
1414

1515
/**
1616
* Run the database seeds.
@@ -22,31 +22,47 @@ public function run()
2222
$tweets = Tweet::all();
2323
$users = User::all();
2424
$comments = Comment::all();
25+
$now = Carbon::now();
2526

2627
$tweetLikes = $tweets->map(
27-
function(Tweet $tweet) use($users) {
28-
$likesCount = random_int(self::MIN_LIKES_COUNT, self::MAX_LIKES_COUNT);
29-
30-
return factory(Like::class, $likesCount)->make([
31-
'user_id' => $users->random()->id,
32-
'likeable_id' => $tweet->id,
33-
'likeable_type' => Tweet::class,
34-
]);
28+
function(Tweet $tweet) use($users, $now) {
29+
$userIds = $users->shuffle()->shuffle()->take(self::LIKES_COUNT)->pluck('id');
30+
31+
return $userIds->map(
32+
function(int $userId) use($tweet, $now) {
33+
return [
34+
'user_id' => $userId,
35+
'likeable_id' => $tweet->id,
36+
'likeable_type' => Tweet::class,
37+
'created_at' => $now->toDateTimeString()
38+
];
39+
}
40+
);
3541
}
3642
);
3743

3844
$commentLikes = $comments->map(
39-
function(Comment $comment) use($users) {
40-
$likesCount = random_int(self::MIN_LIKES_COUNT, self::MAX_LIKES_COUNT);
41-
42-
return factory(Like::class, $likesCount)->make([
43-
'user_id' => $users->random()->id,
44-
'likeable_id' => $comment->id,
45-
'likeable_type' => Comment::class,
46-
]);
45+
function(Comment $comment) use($users, $now) {
46+
$userIds = $users->shuffle()->shuffle()->take(self::LIKES_COUNT)->pluck('id');
47+
48+
return $userIds->map(
49+
function(int $userId) use($comment, $now) {
50+
return [
51+
'user_id' => $userId,
52+
'likeable_id' => $comment->id,
53+
'likeable_type' => Comment::class,
54+
'created_at' => $now->toDateTimeString()
55+
];
56+
}
57+
);
4758
}
4859
);
4960

50-
DB::table('likes')->insert($tweetLikes->flatten()->merge($commentLikes->flatten())->toArray());
61+
DB::table('likes')->insert(
62+
$tweetLikes
63+
->flatten(1)
64+
->merge($commentLikes->flatten(1))
65+
->toArray()
66+
);
5167
}
5268
}

0 commit comments

Comments
 (0)