feat: add comments for controller and model and blade for dashboard for user

This commit is contained in:
2026-08-18 14:26:23 +03:30
parent 5c1af64281
commit 7cd7c804ec
4 changed files with 175 additions and 0 deletions
@@ -0,0 +1,39 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\CommentPostRequest;
use App\Models\Comment;
use App\Models\Tour;
use App\Models\Flight;
class CommentController extends Controller
{
public function store(CommentPostRequest $request)
{
if ($request->commentable_type === 'tour') {
$modelClass = Tour::class;
} else {
$modelClass = Flight::class;
}
$guestName = null;
if (!auth()->check()) {
$guestName = $request->guest_name;
}
Comment::create([
'user_id' => auth()->id(),
'guest_name' => $guestName,
'commentable_id' => $request->commentable_id,
'commentable_type' => $modelClass,
'body' => $request->body,
]);
return back()->with('success', 'نظر شما با موفقیت ثبت شد.');
}
public function index()
{
$comments = auth()->user()->comments()->with('commentable')->latest()->get();
return view('dashboard.comments', compact('comments'));
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class CommentPostRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
$rules = [
'commentable_id' => 'required|integer',
'commentable_type' => 'required|string|in:tour,flight',
'body' => 'required|string|max:1000',
];
if (!auth()->check()) {
$rules['guest_name'] = 'required|string|max:150';
}
return $rules;
}
}