PHP如何将表单输入与数据库值进行比较| Laravel

时间:2017-10-14 08:48:25

标签: php database forms laravel

我需要将数据库值与Form中的输入进行比较,这是我的代码的一部分

 public function update($id, Request $request)
{
    $requestData = $request->all();
    $website_info = WebsiteInfos::findOrFail($id);

        if ( $website_info->all() == $request->all()) {
            Session::flash('alert-info', 'No Change have been made');
            return redirect('admin/website_infos');

        } else {

           $website_info->update($requestData);

            Session::flash('alert-success', 'WebsiteInfos updated!');

            return redirect('admin/website_infos');

        }



}

我需要将$ request-> all()与db值进行比较!我试试这个! if被忽略并始终显示成功警报

2 个答案:

答案 0 :(得分:0)

我会这样做。由于存在主键,日期或外键,数据库记录永远不能等于传入请求。您应该按照以下方法选择要与请求进行比较的数据库值

$db_records =$website_info->pluck(['name', 'url','any_other_field'])->toArray(); 
$incoming_request =$request->all(); //assumes this contains name, url, and any_other_field

if ( $db_records == $incoming_request) {
            Session::flash('alert-info', 'No Change have been made');
            return redirect('admin/website_infos');

        }  

答案 1 :(得分:0)

最简单的方法是在查询中查找记录。

$website_info = WebsiteInfos::where([
    ['id', '=' ,$id],
    ['name', '=', $request->name],
    ['email', '=', $reqest->email]
])->first();

if (count($website_info) > 0) {
    Session::flash('alert-info', 'No Change have been made');
    return redirect('admin/website_infos');
} else {
    $website_info->update($requestData);
    Session::flash('alert-info', 'No Change have been made');
    return redirect('admin/website_infos');
}