I'm trying to convert seats to an array in the controller.
this is how the seats data is sent to the controller seats : "B7D7G10"
i want to convert it to look something like this "B7 , D7 , G10" and I want to match the seats data with my seats table so i can change the seats values in the seats table
This is my jQuery function.
$("#purchase").on("click",function() {
var data = {
seats: $(".seats-selected").text(),
seats_id: $(".seats-selected").data('sid'),
theatre_id: $("#theatres").data('tid'),
movie_id: $(".movie_id").val(),
movie: $("#m-title").text(),
theatre: $("#theatres").text(),
date: $("#date").text(),
time: $("#time").val(),
total_price: $("#total-p").text(),
};
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "POST",
url: "/send-email",
data: data,
success: function(response) {
alert("Your Theatre" + data.theatre + "\n" + data.seats_id + "\n" + data.date + "\n" + data.time + "\n" + data.total_price + "\n" );
}
});
});
This is my controller:
$seats = $request->seats;
$theatre_id = $request->theatre_id;
$date = $request->date;
$time = $request->time;
$movie_id = $request->movie_id;
$total_price = $request->total_price;
$bookings = bookings::where('theatre_id', $theatre_id)
->where('date', $date)
->where('time', $time)
->where('movie_id', $movie_id)
->get();
$reserved_seats = $bookings->pluck('seat_number')->toArray();
$seat = seats::where('seat_number', $request->seats_id)
->where('theatre_id', $theatre_id)
->where('movie_id', $movie_id)
->first();
if ($seat && !in_array($seat->id, $reserved_seats)) {
$seat->available = 0;
$seat->save();
// add the booking to the bookings table
$booking = new bookings;
$booking->theatre_id = $theatre_id;
$booking->movie_id = $movie_id;
$booking->seat_number = explode(',',$seats);
$booking->date = $date;
$booking->time = $time;
$booking->total_price = $total_price;
$booking->user_id = auth()->id();
$booking->save();
I tried every method implode, explode, array_map, everything it won't work if i just leave it be it will only change the value of the first seat. What I wanna do is save the values in bookings table (seat_number column) e.g like this: A1, B9, G10 and match the seats the AJAX request is sending to the controller from the seats table and change its available value to 0.
explode(',', $seats);is not going to work on"B7D7G10"; there's no,separating them. If you sent"B7,D7,G10", then thatexplode()call would convert it to an array. But still, I'm not sure what you're trying to do with your data, or what yourbookingstable looks like, etc., so we won't be able to help much beyond pointing out syntax issues, sorrywhereIn()needs an array, but you've only got Strings. You're gonna have to have a look at your front-end. Right now you're just doingseats: $(".seats-selected").text(), which will sendA1B2C3(or similar), since that's what.text()returns. You have to change that, somehow... I can't see your front-end code, so I have no idea how you need to change that, but you do. Do some searching on how to either manipulate strings in JS, or how to send an Array via AJAX (which is possible), and then you won't need to doexplode()on the back-end at all.