Search code examples
phpmysqllaravellaravel-5laravel-5.3

Laravel: SQL update current time


First Laravel Project. I have a controller, where I upload an image and edit two cell in a mysql row (img and current time) then return to ProductDetails page.

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]);
    }

My problem is that Laravel don't understand the "CURRENT_TIME()"

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

Do I need additional dependency or Laravel has a built-in method for that? (I have to use it on several places)

EDIT: lastchange is a DATETIME cell


Solution

  • Laravel uses Carbon. to get the current time just do this

    import the namespace: use Carbon\Carbon;

    Create the variable: $now = Carbon::now();

    and now your query should look like this:

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