cancel booking option in laravel-8 booking system

196 Views Asked by At

I need help adding Cancel Option for an existing booking, I have $service, booking_code, min_before_cancel, start_date, and status. I want to have the button active ONLY if min_before_cancel !=0 otherwise booking is non-refundable. and the button cancel should be active still time limit (min_before_cancel) if the start_date and today = or less than min_before_cancel the button should disappear.

I have added this in the View

@if ($service =booking->service->time_before_cancel != 0 && \Carbon\Carbon::now() >$booking->start_date->subDays($service->min_before_cancel))
<button class="btn btn-danger">Cancel</button>
@endif

and in the BookingController the following function:

public function cancel($code)
{
// Retrieve the booking from the database
$booking = Booking::find($code);

 // Update the booking status
$booking->status = 'cancelled';
$booking->save();

 // Return a success message to the user
return redirect()->back()->with('success', 'Your booking has been canceled.');
}
public function showCancelOption($bookingCode)
{
// Retrieve the booking from the database
$booking = Booking::where('booking_code', $bookingCode)->first();

// Retrieve the service associated with the booking
$service = $booking->service;

// Calculate the difference in days between the start date and today
$startDate = new Carbon($booking->start_date);
$today = Carbon::now();
$diffInDays = $startDate->diffInDays($today);

}
}

in the Route:

 Route::post('/booking/{code}/cancel', 'BookingController@cancel')->name('booking.cancel');

and new cancel.blade under the booking folder.

Booking Modl

class Boat extends Bookable
{
use Notifiable;
use SoftDeletes;
use CapturesService;

protected $table = 'bravo_boats';
public $type = 'boat';
public $checkout_booking_detail_file       = 'Boat::frontend/booking/detail';
public $checkout_booking_detail_modal_file = 'Boat::frontend/booking/detail-modal';
public $set_paid_modal_file                = 'Boat::frontend/booking/set-paid-modal';
public $email_new_booking_file             = 'Boat::emails.new_booking_detail';
public $availabilityClass = BoatDate::class;

protected $fillable = [
    'title',
    'content',
    'category_id',
    'cancellation_id',
    'style_id',
    'capt_id',
    'health_id',
    'status',
    'manufacturer',
    'reference',
    'crew',
    'model',
    'faqs'
];
protected $slugField     = 'slug';
protected $slugFromField = 'title';
protected $seo_type = 'boat';

protected $casts = [
    'faqs'  => 'array',
    'specs'  => 'array',
    'category'=>'array',
    'cancellation'=>'array',
    'style'=>'array',
    'capt'=>'array',
    'health'=>'array',
    'extra_price'  => 'array',
    'service_fee'  => 'array',
    'price'=>'float',
    'sale_price'=>'float',
];

Appreciate your help

0

There are 0 best solutions below