Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.

Commit d5d9390

Browse files
committed
Cascades
1 parent 2e28ca8 commit d5d9390

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

app/Livewire/Post/Feed.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public function render()
3636
if (auth()->user()->blockedUsers) {
3737
$blockedUsers = auth()->user()->blockedUsers->pluck('blocked_users')->toArray();
3838
$blockedUsers = array_map('trim', explode(',', implode(',', $blockedUsers)));
39+
// Filter out empty strings
40+
$blockedUsers = array_filter($blockedUsers);
3941
}
4042
}
4143

@@ -46,8 +48,7 @@ public function render()
4648
->paginate(20);
4749
} else {
4850
// Normal production query with filters
49-
$posts = Post::with(['user', 'comments', 'likes', 'images'])
50-
->whereNotIn('posts.user_id', $blockedUsers)
51+
$query = Post::with(['user', 'comments', 'likes', 'images'])
5152
->leftJoin('followers', function($join) use ($userId) {
5253
$join->on('posts.user_id', '=', 'followers.following_id')
5354
->where('followers.follower_id', '=', $userId);
@@ -63,8 +64,14 @@ public function render()
6364
})
6465
->select('posts.*')
6566
->distinct()
66-
->orderByDesc('created_at')
67-
->paginate(20);
67+
->orderByDesc('posts.created_at');
68+
69+
// Only add blocked users filter if there are actual blocked users
70+
if (!empty($blockedUsers)) {
71+
$query->whereNotIn('posts.user_id', $blockedUsers);
72+
}
73+
74+
$posts = $query->paginate(20);
6875
}
6976

7077
$parsedown = new Parsedown();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
// Add cascade deletes to posts table
15+
Schema::table('posts', function (Blueprint $table) {
16+
$table->dropForeign(['user_id']);
17+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
18+
});
19+
20+
// Add cascade deletes to likes table
21+
Schema::table('likes', function (Blueprint $table) {
22+
$table->dropForeign(['post_id']);
23+
$table->dropForeign(['user_id']);
24+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
25+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
26+
});
27+
28+
// Add cascade deletes to comments table
29+
Schema::table('comments', function (Blueprint $table) {
30+
$table->dropForeign(['post_id']);
31+
$table->dropForeign(['user_id']);
32+
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
33+
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
34+
});
35+
36+
// Add cascade deletes to followers table
37+
Schema::table('followers', function (Blueprint $table) {
38+
$table->dropForeign(['following_id']);
39+
$table->dropForeign(['follower_id']);
40+
$table->foreign('following_id')->references('id')->on('users')->onDelete('cascade');
41+
$table->foreign('follower_id')->references('id')->on('users')->onDelete('cascade');
42+
});
43+
}
44+
45+
/**
46+
* Reverse the migrations.
47+
*/
48+
public function down(): void
49+
{
50+
// Restore original foreign keys without cascade
51+
Schema::table('posts', function (Blueprint $table) {
52+
$table->dropForeign(['user_id']);
53+
$table->foreign('user_id')->references('id')->on('users');
54+
});
55+
56+
Schema::table('likes', function (Blueprint $table) {
57+
$table->dropForeign(['post_id']);
58+
$table->dropForeign(['user_id']);
59+
$table->foreign('post_id')->references('id')->on('posts');
60+
$table->foreign('user_id')->references('id')->on('users');
61+
});
62+
63+
Schema::table('comments', function (Blueprint $table) {
64+
$table->dropForeign(['post_id']);
65+
$table->dropForeign(['user_id']);
66+
$table->foreign('post_id')->references('id')->on('posts');
67+
$table->foreign('user_id')->references('id')->on('users');
68+
});
69+
70+
Schema::table('followers', function (Blueprint $table) {
71+
$table->dropForeign(['following_id']);
72+
$table->dropForeign(['follower_id']);
73+
$table->foreign('following_id')->references('id')->on('users');
74+
$table->foreign('follower_id')->references('id')->on('users');
75+
});
76+
}
77+
};

0 commit comments

Comments
 (0)