laravel 如何在laravel中使用sql NOW()函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26674288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use sql NOW( ) function in laravel
提问by Ronser
I have used sql's now()
function several times in normal query in php db connectivity...
我now()
在php db连接中的正常查询中多次使用sql的函数......
But in laravel it does not displays anything and also the data is not inserted
但是在laravel中它不显示任何内容,也没有插入数据
the code I am using is:
我使用的代码是:
$exercise = Exercise::insert(
array( 'fk_users_id' => '1',
'fk_equipments_id' => $equipment,
'muscular_group' => $muscular,
'exercise_name' => $postedContent['exercise_name'],
'exercise_type' => $postedContent['type'],
'Status' => '1',
'DateCreated' => 'NOW( )',
'DateModified' => 'NOW( )')
);
it stores the data into the database but the time in not stored in any of the columns
它将数据存储到数据库中,但时间未存储在任何列中
at first my method was:
起初我的方法是:
$exerciseDetails = Input::all();
$exercise = Exercise::create($exerciseDetails);
the above method updates date and time itself and worked fine for other controllers but now I am having a problem of having a check box checked array in my post so it throws error
上面的方法更新日期和时间本身并且对其他控制器工作正常,但现在我在我的帖子中有一个复选框选中数组的问题,所以它抛出错误
is there any other method in laravel to do this ??
Laravel 中还有其他方法可以做到这一点吗?
回答by Bowersbros
Laravel comes with created_at and updated_at timestamp fields by default, which it keeps updated by itself.
Laravel 默认带有 created_at 和 updated_at 时间戳字段,它会自行更新。
回答by Daud khan
$now = DB::raw('NOW()');
$now variable can use in insert like
$now 变量可以像插入一样使用
$user = new User;
$user->name = $name;
$user->created_at = $now;
回答by PHPCian
You could be looking to direct queryto get MySQLDB current Time.
您可能希望直接查询以获取MySQLDB current Time。
You can go like this;
你可以这样去;
$currentDBtime = DB::select( 'select NOW() as the_time' );
$currentDBtime[0]->the_time;