Php 仅当用户更改laravel中的任何内容时才更新数据

Php 仅当用户更改laravel中的任何内容时才更新数据,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,仅当用户更改任何内容时才更新数据,否则将返回一条消息,说明无需更新任何内容。单击“编辑”按钮后,如果用户点击“更新”按钮而未更改任何内容,则返回消息“无需更新”。 如果用户更改了编辑表单中的任何内容,则该表单将被更新 我试过这个 ( I have multiple fields. This is for demo) if ($user->name == $request->name && $user->email == $request->

仅当用户更改任何内容时才更新数据,否则将返回一条消息,说明无需更新任何内容。单击“编辑”按钮后,如果用户点击“更新”按钮而未更改任何内容,则返回消息“无需更新”。 如果用户更改了编辑表单中的任何内容,则该表单将被更新

我试过这个

( I have multiple fields. This is for demo)
   
    if ($user->name == $request->name && $user->email == $request->email && $user->contact == 
       $request>contact) {
            return back()->with('info', 'You have not change anything. Nothing to update!');

        } else {
            $user->update([
                'name'    => $request->name,
                'email'   => $request->email,
                'contact' => $request->contact,
                'image'   => $image,
            ]);

        }
        return redirect()->route('admin.home')->with('success', 'Profile has been updated');
那件作品很好,除了图像

这是我的图片上传过程

 if ($request->has('image')) {
     Storage::delete('public/avatar/users/' . $user->image);
      $image_name = hexdec(uniqid());
      $ext  = strtolower($request->image->getClientOriginalExtension());
      $image_full_name = $image_name . '.' . $ext;
      $request->image->storeAs('avatar/users/', $image_full_name, 'public');
      $image = $image_full_name;
        } else {
            $image = $user->image;
        }

如何使用图像来执行此操作?最好的方法是什么?

由于Elount可以跟踪对模型所做的更改,因此我建议使用来确定模型是否正确

$updatedUser = $user->update([
    'name' => $request->name,
    'email' => $request->email,
    'contact' => $request->contact,
    'image' => $image,
]);
if (serialize($user) == serialize($updatedUser)) {
   // user has updated something
} else {
  // no changes were made
}
当原始脚本处理上载的文件并将$user->image作为字符串持久化到数据库时,您需要将上载的文件源与当前的图像文件源(如果有)进行比较,以确定是否应应用图像更改

如果$request->具有“图像”{ //仅检查提供的图像 如果!$user->image | sha1|u文件存储::path'public/avatar/users/。$user->image!==sha1|u文件$request->image->path{ //应用图像更改 如果$user->image{ //删除以前的图像 存储::删除“public/avatar/users/”。$user->image; } $image=str_replace'.',uniqid,true'..。strtolower$request->image->getClientOriginalExtension; $request->image->storeAs'avatar/users/',$image'public'; $user->fill['image'=>$image]; } } $user->fill[ “名称”=>$request->name, “电子邮件”=>$request->email, “联系人”=>$request->contact, ]; $user->save; 如果$user->被更改['name','email','contact','image']{ return redirect->route'admin.home'->带有'success','Profile已经更新'; } return back->with'info',您没有更改任何内容。无需更新; 免责声明 我不建议使用hextecuniqid,而是直接使用uniqid。因为hexdec的使用将忽略它遇到的任何非十六进制字符,并可能导致不希望的结果或数据丢失

例如:

var_Dumpsee; var_Dumphextecee; //两者都打印int238
什么是$image和$user->$image?路径?$image=$request->image,那么什么是$request->image?服务器上图像的路径?不,来自编辑表单的图像请发布处理图像上载过程的代码,因为我们无法确定您是否正在使用LOB或处理上载并将其移动到路径,例如使用原始文件名、uniqid或它是否只是URL等。我应用了您的方法,如果我改变了什么,那么我得到了这个//没有改变,如果我没有改变任何东西,那么也得到了这个//没有改变made@ihprince逻辑也是颠倒的,如果两个值相等,则没有任何更改。您的结果是由和返回布尔值引起的。因此serializeobject==serializebool将始终为false。