git commit "add comments and fix tour comments display"

This commit is contained in:
2026-08-17 13:33:37 +03:30
parent 970786ae94
commit 902384625b
11 changed files with 729 additions and 166 deletions
@@ -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',
'نظر شما با موفقیت ثبت شد و پس از بررسی نمایش داده می‌شود.'
);
}
}
+6 -1
View File
@@ -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'));
}
}
+6 -1
View File
@@ -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'));
}
}
+53
View File
@@ -0,0 +1,53 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
class CommentStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, ValidationRule|array<mixed>|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',
],
];
}
}
+18 -11
View File
@@ -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');
}}
+52 -42
View File
@@ -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);
});
}
}
}
+2
View File
@@ -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',
+4
View File
@@ -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');
}
}