Laravel: SQL 更新当前时间

Laravel: SQL update current time

第一个 Laravel 项目。我有一个控制器,我在其中上传图像并编辑 mysql 行中的两个单元格(img 和当前时间),然后 return 到 ProductDetails 页面。

public function uploadImage(Request $request, $id){
    $file = $request->file('image');
    //Display File Name
    echo 'File Name: '.$file->getClientOriginalName();
    echo '<br>';
    //Display File Extension
    echo 'File Extension: '.$file->getClientOriginalExtension();
    echo '<br>';
    //Display File Real Path
    echo 'File Real Path: '.$file->getRealPath();
    echo '<br>';
    //Display File Size
    echo 'File Size: '.$file->getSize();
    echo '<br>';
    //Display File Mime Type
    echo 'File Mime Type: '.$file->getMimeType();
    //Move Uploaded File
    $destinationPath = 'media/productimg';
    $file->move($destinationPath,$file->getClientOriginalName());
    $img=$file->getClientOriginalName();
    DB::update('update inventory set img = ? lastchange = ? where barcode = ?',[$img,CURRENT_TIME(),$id]);
    $product = DB::select('select * FROM inventory WHERE barcode = ?', [$id]);
    return view('productdetails',['product'=>$product]);
    }

我的问题是 Laravel 不理解 "CURRENT_TIME()"

FatalThrowableError in InventoryController.php line 47: Call to undefined function App\Http\Controllers\CURRENT_TIME()

我是否需要额外的依赖项或 Laravel 有一个内置的方法? (我必须在几个地方使用它)

编辑: lastchange 是 DATETIME 单元格

Laravel 使用 Carbon。 要获取当前时间,只需执行此操作

导入命名空间: use Carbon\Carbon;

创建变量: $now = Carbon::now();

现在您的查询应如下所示:

DB::update('update inventory set img = ? lastchange = ? where barcode = ?',[$img,$now,$id]);

尼考拉斯的 seems to be good solution but as another approach is to use mysql internal now() function or it's alternatives

DB::update('update inventory set img = ? lastchange = NOW() where barcode = ?',[$img,$id]);