5
5
use App \Models \Comment ;
6
6
use Illuminate \Http \Request ;
7
7
use App \Models \User ;
8
+ use Illuminate \Support \Facades \Auth ;
8
9
9
10
class ProfileCommentsController extends Controller
10
11
{
12
+ public string $ action = 'append ' ;
13
+
11
14
/**
12
15
* @param \App\Models\User $user
13
16
* @param array $data
14
17
*
15
18
* @return string
16
19
*/
17
- public function show (User $ user , array $ data = [])
20
+ public function show (Request $ request , User $ user , array $ data = [])
18
21
{
22
+ $ comments = $ this ->getComments ($ user );
23
+ $ comments ->withPath ('/profile/ ' .$ user ->nickname .'/comments ' );
24
+
25
+ if ($ request ->get ('page ' ) > 1 && $ comments ->isEmpty ())
26
+ {
27
+ $ request ->merge ([
28
+ 'page ' => $ comments ->lastPage (),
29
+ ]);
30
+ $ comments = $ this ->getComments ($ user );
31
+ }
32
+
33
+ $ isMyAccount = $ user ->id === Auth::user ()?->id;
19
34
return view (
20
35
'pages.profile.tabs.comments-tab ' ,
21
36
array_merge ($ data , [
22
- 'comments ' => $ user -> comments ()-> latest ()-> get () ,
37
+ 'comments ' => $ comments ,
23
38
'user ' => $ user ,
24
- 'active ' => 'comments '
39
+ 'active ' => 'comments ' ,
40
+ 'action ' => $ this ->action ,
41
+ 'isMyAccount ' => $ isMyAccount
25
42
])
26
- )->fragmentIf (!request ()->isMethod ('GET ' ), ' comments ' );
43
+ )->fragmentsIf (!request ()->isMethod ('GET ' ));
27
44
}
28
45
46
+ protected function getComments (User $ user ){
47
+ return $ comments = $ user ->comments ()
48
+ ->orderBy ('id ' , 'desc ' )
49
+ ->paginate (2 );;
50
+ }
29
51
30
52
/**
31
53
* @param \App\Models\Comment $comment
32
54
*
33
55
* @return string
34
56
*/
35
- public function showEdit (Comment $ comment )
57
+ public function showEdit (Request $ request , Comment $ comment )
36
58
{
37
- return $ this ->show ($ comment ->commenter , [
59
+ return $ this ->show ($ request , $ comment ->commenter , [
38
60
'edit ' => $ comment ->getKey (),
39
61
]);
40
62
}
@@ -58,23 +80,23 @@ public function update(Request $request, Comment $comment)
58
80
'comment ' => $ request ->message ,
59
81
]);
60
82
61
- return $ this ->show ($ comment ->commenter );
83
+ return $ this ->show ($ request , $ comment ->commenter );
62
84
}
63
85
64
86
/**
65
87
* @param \App\Models\Comment $comment
66
88
*
67
89
* @return string
68
90
*/
69
- public function delete (Comment $ comment )
91
+ public function delete (Request $ request , Comment $ comment )
70
92
{
71
93
$ this ->authorize ('delete ' , $ comment );
72
94
73
95
$ comment ->children ()->exists ()
74
96
? $ comment ->delete ()
75
97
: $ comment ->forceDelete ();
76
98
77
- return $ this ->show ($ comment ->commenter );
99
+ return $ this ->show ($ request , $ comment ->commenter );
78
100
}
79
101
80
102
}
0 commit comments