在我的组页面中,我正在创建新的组ID,当我自动创建组ID时,时间需要存储到数据库中,列creationTime。我正在使用laravel 5.2框架来构建我的应用程序。在向表中插入groupID时,我如何才能占用创建时间并将其存储到数据库中。我的GroupController.php
public function groupInsert()
{
$postGeozone=Input::all();
//insert data into mysql table
$account = Account::select('accountID')->get();
foreach ($account as $acc) {
$abc = $acc->accountID;
}
$data = array(
"accountID" => $abc,
"groupID"=> $postGeozone['groupID']
);
//$data=array('groupID'=> $postGeozone['groupID']);
$ck=0;
$ck=DB::table('devicegroup')->insert($data);
$groups = DB::table('devicegroup')->simplePaginate(5);
return view('group.groupAdmin')->with('groups',$groups);
}
groupAdmin.blade.php是
@extends('app')
@section('content')
<div class="templatemo-content-wrapper">
<div class="templatemo-content">
<ol class="breadcrumb">
<li><a href="{{ url("/") }}"><font color="green">Home</font></a></li>
<li class="active">Group information</li>
</ol>
<h1>View/Edit Group information</h1>
<p></p>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table id="example" class="table table-striped table-hover table-bordered">
<h3>Select a Group :</h3>
<thead>
<tr>
<th>Group ID</th>
<th>Group Name</th>
<th>Vehicle Count</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach($groups as $grp)
<tr>
<td>{{ $grp->groupID }}</td>
<td>{{ $grp->description }}</td>
<td></td>
<td>
<div class="btn-group">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
{{--@if ($nam->isActive == 'Yes')--}}
<li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $grp->groupID }}"><a href="{{ url('/group/edit/'.$grp->groupID) }}">View/ Edit</a>
</li>
{{--@endif--}}
<li><a href="{{ url('/group/delete/'.$grp->groupID)}}">Delete</a></li>
</ul>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
{{$groups->links()}}
</div>
</div>
</div>
</div>
</div>
</br>
{{--Creating new group--}}
{{--------------------------------------------------}}
<h4>Create a new Group</h4>
<form role="form" method="POST" action="{{ url('groupAdmin') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6 margin-bottom-15">
<input type="text" class="form-control" name="groupID" value="{{ old('groupID') }}" placeholder="Enter Group ID">
</div>
<div class="row templatemo-form-buttons">
<div class="submit-button">
<button type="submit" class="btn btn-primary">New</button>
</div>
</div>
</div>
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
@endsection
routes.php
Route::any('groupAdmin', 'GroupController@getIndex');
Route::post('groupAdmin', 'GroupController@groupInsert');
model Group.php
<?php
namespace App;
use Illuminate'Database'Eloquent'Model;
class Group extends Model
{
protected $table = 'devicegroup';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'groupID', 'description',
];
protected $hidden = [
'password', 'remember_token',
];
}
谁能告诉我怎么做吗,答复是可观的。
使用
Schema::table('devicegroup', function (Blueprint $table) {
$table->timestamps();
});
在您的迁移中,Laravel将为您提供inserted_at和updated_at,并且在您使用模型插入数据时,它还将负责为您更新这些列。您将不需要添加任何代码来设置日期。
此外,您有一个Group模型,但您没有在控制器中使用它。您可以使用Group::create($data);
和类似的方法来获取数据,而不是DB::table('devicegroup')->insert($data);
。
选择Mysql日期类型时间戳取消默认值CURRENT_Timestamp
示例:
ALTER TABLE `tbl_name` ADD `cdate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
if使用模型的工作非常简单,并且创建和更新时间是自动更新的。
1-创建名为Group的模型并扩展模型2-添加到数据库设备组表中的created_at和updated_at文件3-用于将新数据添加到此用途的以下代码:
$group = new Group();
$group->accountID = $abc;
$group->groupID = $postGeozone['groupID'];
$group->save();
并自动插入created_at文件。