您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页laravel查询重复的数据,laravel防止重复记录

laravel查询重复的数据,laravel防止重复记录

来源:宝玛科技网

How to properly check if a data already exist in DB and return message into view

$check = PropertyReservation::where('res_id', Auth::user()->res_id)->where('property_no',$reservation->property_no)->first();

if($check){

return redirect('/reservations')->with('success','You have an existing reservation for this item ' );

}

else{

$reservation->reservation_name = $resident->resident_fname;

$reservation->reservation_type = $request->input('reservation_type');

$reservation->reservation_quantity = $request->input('reservation_quantity');

$reservation->save();

}

return redirect('/reservations');

解决方案

Your check will work as well, as if there is no reservation for your query it returns null which means your else block will execute. But there is a better method on the query builder called exists. So you can try this as well:

if(PropertyReservation::where('res_id', Auth::user()->res_id)->where('property_no',$reservation->property_no)->exists();

{

// redirect back

}

// no need for else block as this will execute if the condition above is false

// save the reservation

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务