gpt4 book ai didi

php - Laravel:SQL更新当前时间

转载 作者:行者123 更新时间:2023-11-29 04:16:35 24 4
gpt4 key购买 nike

第一个 Laravel 项目。我有一个 Controller ,我在其中上传图像并编辑 mysql 行中的两个单元格(img 和当前时间),然后返回到 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 .要获取当前时间,只需执行此操作

导入命名空间: 使用 Carbon\Carbon;

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

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

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

关于php - Laravel:SQL更新当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42553332/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com