我如何在Laravel 8中编辑房间预订。控制器的更新不起作用

发布于 2025-02-04 07:16:43 字数 3111 浏览 3 评论 0原文

控制器(更新):您可以在更新中看到,我试图编辑房间预订(时间和日期),但一部分可以使用IF或其他!

public function update(Request $request,  Roomreservation $roomreservation)
    {
        $request->validate([
            'date' => 'required',
            'time' => 'required',
        ]);
        $roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
        if ($roomreservation->exists()) {
            return back()->with('erreur', 'The chosen room is already reserved');
        }
        else {
           $roomreservation->update([
                "date" => $request->date,
               "time" => $request->time,
           ]);
            return back()->with('message_sent', 'room reservation edited successfully!');
        }

    }

我的表单

 <form method="post" action="{{ route('admin.roomreservations.update', $roomreservation->id) }}">
                                    @csrf
                                    @method('PUT') @method('PUT')
                                    <div>
                                        <div>
                                            <label for="date">Date</label>
                                            <input type="date" min="2022-01-01" max="2022-12-31" name="date" id="date" 
                                                   value="{{ old('date', $roomreservation->date) }}" />
                                        </div>
                                        <div>
                                            <label for="time">Time</label>
                                            <select name="time" id="time" 
                                                    value="{{ old('time',$roomreservation->time) }}" >
                                                <option>8H30-10H00</option>
                                                <option>10H00-11H30</option>
                                                <option>11H30-13H00</option>
                                            </select>
                                        </div>
                                        <div>
                                            <button>
                                                Edit
                                            </button>
                                        </div>
                                    </div>
                                </form>

索引表格上的按钮更新

<a href="{{ route('admin.roomreservations.edit', $roomreservation->id) }}" >Update</a>

我认为更新条件上有问题

$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
            if ($roomreservation->exists()) 

controller(update) :as you can see in the update i'm trying to edit the room reservation(time and date),but onlly one part work the if or the else!

public function update(Request $request,  Roomreservation $roomreservation)
    {
        $request->validate([
            'date' => 'required',
            'time' => 'required',
        ]);
        $roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
        if ($roomreservation->exists()) {
            return back()->with('erreur', 'The chosen room is already reserved');
        }
        else {
           $roomreservation->update([
                "date" => $request->date,
               "time" => $request->time,
           ]);
            return back()->with('message_sent', 'room reservation edited successfully!');
        }

    }

my form

 <form method="post" action="{{ route('admin.roomreservations.update', $roomreservation->id) }}">
                                    @csrf
                                    @method('PUT') @method('PUT')
                                    <div>
                                        <div>
                                            <label for="date">Date</label>
                                            <input type="date" min="2022-01-01" max="2022-12-31" name="date" id="date" 
                                                   value="{{ old('date', $roomreservation->date) }}" />
                                        </div>
                                        <div>
                                            <label for="time">Time</label>
                                            <select name="time" id="time" 
                                                    value="{{ old('time',$roomreservation->time) }}" >
                                                <option>8H30-10H00</option>
                                                <option>10H00-11H30</option>
                                                <option>11H30-13H00</option>
                                            </select>
                                        </div>
                                        <div>
                                            <button>
                                                Edit
                                            </button>
                                        </div>
                                    </div>
                                </form>

button update on the index form

<a href="{{ route('admin.roomreservations.edit', $roomreservation->id) }}" >Update</a>

i think something is wrong on the update condition

$roomreservation = Roomreservation::where('date', request('date'))->where('time', request('time'))->where('code_room', request('code_room'));
            if ($roomreservation->exists()) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往昔成烟 2025-02-11 07:16:43

看来您传递了一个错误的参数
将您的更新功能更改为下面

public function update(Request $request,  $id)
{
    $request->validate([
        'date' => 'required',
        'time' => 'required',
    ]);
    $roomreservation = Roomreservation::find($id);

    if ($roomreservation == null) {
        return back()->with('erreur', 'The chosen room does not exist');
    }

    if ($roomreservation->date == $request->date && $roomreservation->time == $request->time && $roomreservation->code_room == $request->code_room) {
        return back()->with('erreur', 'The chosen room is already reserved');
    }
    else {
       $roomreservation->update([
            "date" => $request->date,
           "time" => $request->time,
       ]);
        return back()->with('message_sent', 'room reservation edited successfully!');
    }

}

It seems that you're passing a wrong parameter
Change your update function to the below

public function update(Request $request,  $id)
{
    $request->validate([
        'date' => 'required',
        'time' => 'required',
    ]);
    $roomreservation = Roomreservation::find($id);

    if ($roomreservation == null) {
        return back()->with('erreur', 'The chosen room does not exist');
    }

    if ($roomreservation->date == $request->date && $roomreservation->time == $request->time && $roomreservation->code_room == $request->code_room) {
        return back()->with('erreur', 'The chosen room is already reserved');
    }
    else {
       $roomreservation->update([
            "date" => $request->date,
           "time" => $request->time,
       ]);
        return back()->with('message_sent', 'room reservation edited successfully!');
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文