feat: add comments for controller and model and blade for dashboard for user
This commit is contained in:
@@ -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'));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Created by Reliese Model.
|
||||
*/
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Class Comment
|
||||
*
|
||||
* @property int $id
|
||||
* @property int|null $user_id
|
||||
* @property string|null $guest_name
|
||||
* @property int $commentable_id
|
||||
* @property string $commentable_type
|
||||
* @property string $body
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $updated_at
|
||||
*
|
||||
* @property User|null $user
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class Comment extends Model
|
||||
{
|
||||
protected $table = 'comments';
|
||||
|
||||
protected $casts = [
|
||||
'user_id' => 'int',
|
||||
'commentable_id' => 'int'
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'guest_name',
|
||||
'commentable_id',
|
||||
'commentable_type',
|
||||
'body'
|
||||
];
|
||||
|
||||
public function commentable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user