Search code examples
phplaravellaravel-5.3

Laravel: Script message is not showing


I want to delete a row from my database after clicking a href link. It is working fine. But after completing deletion I want show a message to the user. but it is not showing. my if condition is also not working...it is showing :

Object of class Illuminate\Support\Collection could not be converted to int

My code is here:

<?php
public function postDelete($booked_by) {
    $user_id = Auth::user()->id;
    $booking_id = DB::table('bookings')
            ->select('id')
            ->where('booked_by', '=', $user_id)
            ->get();

    // dd($booking_id);

    if ($user_id == $booking_id) {
        $user = DB::table('bookings')
                ->where('booked_by', '=', $user_id)
                ->delete();

        return back();
        $message = "Your booking is canceled";
        echo "<script type='text/javascript'>alert('$message');       </script>";
        else {
            $message = "You can't delete which is booked by others";
            echo "<script type='text/javascript'>alert('$message');</script>";
        }


        return back();
    }
}
?>

what should I change ?


Solution

  • Please use Flash Message in this case:

    https://laravel.com/docs/5.4/session#flash-data

    If you use alert message it's really dangerous with XSS bugs.