diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index 8919584..2213537 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Http\Requests\CommentStoreRequest; use App\Models\Comment; use Illuminate\Http\Request; @@ -18,4 +19,27 @@ class CommentController extends Controller return view('account.users.comments.index',compact('comments')); } + + public function store(CommentStoreRequest $request) + { + $data = $request->validated(); + + $data['user_id'] = auth()->id(); + $data['status'] = 0; + + Comment::create([ + 'flight_id' => $request->flight_id, + 'tour_id' => $request->tour_id, + 'comment' => $request->comment, + 'rating' => $request->rating, + 'user_id' => auth()->id(), + 'status' => 0, + ]); + + return back()->with( + 'success', + 'نظر شما با موفقیت ثبت شد و پس از بررسی نمایش داده می‌شود.' + ); + + } } diff --git a/app/Http/Controllers/FlightController.php b/app/Http/Controllers/FlightController.php index a832c84..25be11a 100644 --- a/app/Http/Controllers/FlightController.php +++ b/app/Http/Controllers/FlightController.php @@ -24,7 +24,12 @@ class FlightController extends Controller { $flight->increment('views'); - return view('flights.show', compact('flight')); + $comments = $flight->comments() + ->latest() + ->get(); + + + return view('flights.show', compact('flight','comments')); } } diff --git a/app/Http/Controllers/TourController.php b/app/Http/Controllers/TourController.php index 9a06251..7de2338 100644 --- a/app/Http/Controllers/TourController.php +++ b/app/Http/Controllers/TourController.php @@ -24,7 +24,12 @@ class TourController extends Controller public function show(Tour $tour) { $tour->load('travelType'); + $tour->increment('views'); + $comments = $tour->comments() + ->latest() + ->get(); - return view('tours.show', compact('tour')); + + return view('tours.show', compact('tour','comments')); } } diff --git a/app/Http/Requests/CommentStoreRequest.php b/app/Http/Requests/CommentStoreRequest.php new file mode 100644 index 0000000..738c657 --- /dev/null +++ b/app/Http/Requests/CommentStoreRequest.php @@ -0,0 +1,53 @@ +|string> + */ + public function rules(): array + { + return [ + 'tour_id' => [ + 'nullable', + 'exists:tours,id', + 'required_without:flight_id', + ], + + 'flight_id' => [ + 'nullable', + 'exists:flights,id', + 'required_without:tour_id', + ], + + 'rating' => [ + 'required', + 'integer', + 'min:1', + 'max:5', + ], + + 'comment' => [ + 'nullable', + 'string', + 'max:1000', + ], + + ]; + } +} diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 6233565..03ed3d9 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -11,16 +11,17 @@ use Illuminate\Database\Eloquent\Model; /** * Class Comment - * + * * @property int $id * @property int $user_id * @property int $tour_id + * @property int $flight_id * @property int $rating * @property string|null $comment * @property int $status * @property Carbon|null $created_at * @property Carbon|null $updated_at - * + * * @property Tour $tour * @property User $user * @@ -34,6 +35,7 @@ class Comment extends Model protected $casts = [ 'user_id' => 'int', 'tour_id' => 'int', + 'flight_id' => 'int', 'rating' => 'int', 'status' => 'int' ]; @@ -41,18 +43,23 @@ class Comment extends Model protected $fillable = [ 'user_id', 'tour_id', + 'flight_id', 'rating', 'comment', 'status' ]; - public function tour() - { - return $this->belongsTo(Tour::class); - } + public function tour() + { + return $this->belongsTo(Tour::class, 'tour_id'); + } - public function user() - { - return $this->belongsTo(User::class); - } -} + public function flight() + { + return $this->belongsTo(Flight::class, 'flight_id'); + } + + public function user() + { + return $this->belongsTo(User::class, 'user_id'); + }} diff --git a/app/Models/Flight.php b/app/Models/Flight.php index 90c59bc..f695947 100644 --- a/app/Models/Flight.php +++ b/app/Models/Flight.php @@ -23,10 +23,10 @@ use Illuminate\Database\Eloquent\Model; * @property Carbon|null $departure_time * @property Carbon|null $arrival_time * @property string|null $flight_class + * @property string|null $image + * @property string $views * @property int|null $capacity * @property float|null $price - * @property string|null $image - * @property string|null $views * @property int|null $status * @property Carbon|null $created_at * @property Carbon|null $updated_at @@ -35,49 +35,59 @@ use Illuminate\Database\Eloquent\Model; */ class Flight extends Model { - protected $table = 'flights'; - public static $snakeAttributes = false; + protected $table = 'flights'; + public static $snakeAttributes = false; - protected $casts = [ - 'departure_date' => 'datetime', - 'departure_time' => 'datetime', - 'arrival_time' => 'datetime', - 'capacity' => 'int', - 'price' => 'float', - 'status' => 'int' - ]; + protected $casts = [ + 'departure_date' => 'datetime', + 'departure_time' => 'datetime', + 'arrival_time' => 'datetime', + 'capacity' => 'int', + 'price' => 'float', + 'status' => 'int' + ]; - protected $fillable = [ - 'airline', - 'flight_number', - 'origin', - 'destination', - 'departure_date', - 'departure_time', - 'arrival_time', - 'flight_class', - 'capacity', - 'price', + protected $fillable = [ + 'airline', + 'flight_number', + 'origin', + 'destination', + 'departure_date', + 'departure_time', + 'arrival_time', + 'flight_class', + 'capacity', + 'price', 'image', 'views', - 'status' - ]; + 'status' + ]; - #[Scope] -public function applySearch(Builder $query, array $filters): void -{ - $query - ->when($filters['origin'] ?? null, function ($query, $origin) { - $query->where('origin', $origin); - }) - ->when($filters['destination'] ?? null, function ($query, $destination) { - $query->where('destination', $destination); - }) - ->when($filters['departure_date'] ?? null, function ($query, $date) { - $query->whereDate('departure_date', $date); - }) - ->when($filters['passengers'] ?? null, function ($query, $passengers) { - $query->where('capacity', '>=', $passengers); - }); + public function travelType() + { + return $this->belongsTo(TravelType::class); + } + + public function comments() + { + return $this->hasMany(Comment::class); + } + + #[Scope] + public function applySearch(Builder $query, array $filters): void + { + $query + ->when($filters['origin'] ?? null, function ($query, $origin) { + $query->where('origin', $origin); + }) + ->when($filters['destination'] ?? null, function ($query, $destination) { + $query->where('destination', $destination); + }) + ->when($filters['departure_date'] ?? null, function ($query, $date) { + $query->whereDate('departure_date', $date); + }) + ->when($filters['passengers'] ?? null, function ($query, $passengers) { + $query->where('capacity', '>=', $passengers); + }); + } } - } diff --git a/app/Models/Tour.php b/app/Models/Tour.php index 2e226ea..e9cbe9e 100644 --- a/app/Models/Tour.php +++ b/app/Models/Tour.php @@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Model; * @property string $summary * @property string $description * @property string $image + * @property string $views * @property int $travel_type_id * @property float $price * @property Carbon $departure_date @@ -46,6 +47,7 @@ class Tour extends Model 'summary', 'description', 'image', + 'views', 'travel_type_id', 'price', 'departure_date', diff --git a/app/Models/TravelType.php b/app/Models/TravelType.php index e5fed5c..4902fb3 100644 --- a/app/Models/TravelType.php +++ b/app/Models/TravelType.php @@ -37,4 +37,8 @@ class TravelType extends Model { return $this->hasMany(Tour::class, 'travel_type_id'); } + public function flights() + { + return $this->hasMany(Flight::class, 'travel_type_id'); + } } diff --git a/resources/views/flights/show.blade.php b/resources/views/flights/show.blade.php index 673b2b3..b19dc4e 100644 --- a/resources/views/flights/show.blade.php +++ b/resources/views/flights/show.blade.php @@ -1,161 +1,424 @@ +
@@ -221,6 +484,109 @@ {{ number_format($flight->price) }} تومان
+
+ تعداد مشاهده + + + {{ $flight->views ?? 0 }} + بازدید + +
+ + + +
+ +

+ ثبت نظر درباره این پرواز +

+ +
+ + @csrf + + + +
+ + + + + +
+ +
+ + + + + +
+ + + +
+
+ +

+ نظرات کاربران +

+ + @forelse($comments as $comment) + +
+ +
+ + + {{ $comment->user->name }} + + + + {{ $comment->created_at->diffForHumans() }} + + +
+ +

+ {{ $comment->comment }} +

+ +
+ + @empty + +
+ هنوز نظری ثبت نشده است. +
+ + @endforelse + +
diff --git a/resources/views/tours/show.blade.php b/resources/views/tours/show.blade.php index 50cba9c..c97af2b 100644 --- a/resources/views/tours/show.blade.php +++ b/resources/views/tours/show.blade.php @@ -102,6 +102,92 @@ {{ $tour->description }}

+
+ تعداد مشاهده + + + {{ $tour->views ?? 0 }} + بازدید + +
+ + +
+ + @csrf + + + +
+ + + +
+ +
+ + + +
+ + + +
+
+ +

+ نظرات کاربران +

+ + @forelse($comments as $comment) + +
+ +
+ + + {{ $comment->user->name }} + + + + {{ $comment->created_at->diffForHumans() }} + + +
+ +

+ {{ $comment->comment }} +

+ +
+ + @empty + +
+ هنوز نظری ثبت نشده است. +
+ + @endforelse
diff --git a/routes/web.php b/routes/web.php index 3fb6cc7..2cc5e68 100644 --- a/routes/web.php +++ b/routes/web.php @@ -33,6 +33,7 @@ Route::prefix('users')->name('users.')->middleware('auth:web')->group(function ( Route::delete('/favorite/{favorite}',[FavoriteController::class,'destroy'])->name('favorite.destroy'); Route::get('/comments',[CommentController::class,'index'])->name('comments.index'); + Route::post('/comments',[CommentController::class,'store'])->name('comments.store'); Route::get('/cities/{province}', [CityController::class, 'byProvince'])->name('cities.province');