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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
@extends('dashboard.layouts.app')
|
||||||
|
|
||||||
|
@section('title' , 'نظرات من')
|
||||||
|
|
||||||
|
|
||||||
|
@section('account-content')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="col-lg-9">
|
||||||
|
<div class="user-profile-wrapper">
|
||||||
|
<div class="col-lg-12">
|
||||||
|
<div class="user-profile-card profile-booking">
|
||||||
|
<h4 class="user-profile-card-title">نظرات من ({{ $comments->count() }})</h4>
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table text-nowrap">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>شماره</th>
|
||||||
|
<th>نظر</th>
|
||||||
|
<th>مربوط به</th>
|
||||||
|
<th>تاریخ</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
@forelse($comments as $index => $comment)
|
||||||
|
<tr>
|
||||||
|
<td>{{ $index + 1 }}.</td>
|
||||||
|
<td>
|
||||||
|
<p>{{ $comment->body }}</p>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{ $comment->commentable->title ?? $comment->commentable->flight_number ?? '-' }}
|
||||||
|
</td>
|
||||||
|
<td>{{ $comment->created_at->format('Y/m/d') }}</td>
|
||||||
|
</tr>
|
||||||
|
@empty
|
||||||
|
<tr>
|
||||||
|
<td colspan="4" class="text-center">هنوز نظری ثبت نکردی.</td>
|
||||||
|
</tr>
|
||||||
|
@endforelse
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
@endsection
|
||||||
Reference in New Issue
Block a user