Laravel: 脚本消息未显示

Laravel: Script message is not showing

我想在单击 href link 后从我的数据库中删除一行。它工作正常。但在完成删除后,我想向用户显示一条消息。但它没有显示。我的 if 条件也不起作用...显示:

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

我的代码在这里:

<?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();
    }
}
?>

我应该改变什么?

这种情况下请使用Flash Message

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

如果您使用警告消息,那么 XSS 错误真的很危险。

在 if 条件可能有效的情况下回显消息后移至 return back();。但这不是正确的做法。

正确的方法是像这样通过重定向传递消息

  return back()->with("message","your message");

并在视图中显示消息

  {{ $message or "" }}