【问题标题】:How store create time automatically to the table when we create new ID创建新ID时如何将创建时间自动存储到表中
【发布时间】:2016-12-02 15:05:17
【问题描述】:

在我的群组页面中,我正在创建新的群组 ID,当我自动创建群组 ID 时,该时间需要存储到数据库中,列创建时间。我正在使用 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');

模型组.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',
    ];
}

谁能告诉我怎么做, 回复是可观的。

【问题讨论】:

    标签: php mysqli laravel-5.2


    【解决方案1】:

    选择Mysql Date type Timestamp Default value CURRENT_TIMESTAMP

    示例:

    ALTER TABLE `tbl_name` ADD `cdate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
    

    【讨论】:

    • 如何在我的控制器中使用这些东西
    • 默认情况下它会存储当前时间...只需在表中进行更改
    • 其实在创建groupID的时候需要发送creattionTime
    • creattionTime有两种存储方式 1. 将creattionTime设为datetime类型(mysql date type datetime)并发送当前时间 $creattionTime =date('Y-m-d H:i:s'); 2. Make is TIMESTAMP 默认值为 CURRENT_TIMESTAMP 不需要发送值
    【解决方案2】:

    如果使用模型工作非常简单并且创建和更新时间自动更新。

    1-创建名称为 Group 的模型并扩展模型 2-添加到数据库设备组表中的 created_at 和 updated_at 3-在下面的代码中添加新数据:

    $group = new Group();
    $group->accountID = $abc;
    $group->groupID = $postGeozone['groupID'];
    $group->save();
    

    并自动插入 created_at 文件。

    【讨论】:

    • 是的,我正在使用模型。我将在我的问题中添加模型。但我不明白,我在哪里可以使用这个代码?
    • 而不是$ck=DB::table('devicegroup')-&gt;insert($data); 使用top code 插入表并created_at 自动更新。
    • 这里插入creationTime的代码在哪里?其实我是 laravel 的新手
    • 你能告诉我如何在 laravel 5.2 中将当前 dateTime 值降低到 X 以下吗.. "creationTime"=> $postGeozone['X']
    【解决方案3】:

    使用

    Schema::table('devicegroup', function (Blueprint $table) {
                 $table->timestamps();
             });
    

    在您的迁移中,Laravel 会为您包含inserted_at 和 updated_at,并且当您使用模型插入数据时,它还会为您更新这些列。您无需添加任何代码即可设置日期。

    您还有一个 Group 模型,但您没有在控制器中使用它。您可以使用 Group::create($data); 代替 DB::table('devicegroup')-&gt;insert($data); 来获取数据。

    【讨论】:

    • 如何将字段名从create_at改为creationTime?
    • 你正在扩展的模型有const CREATED_AT = 'created_at';,你可以尝试在那里改变它。或者,如果您可以控制数据库,则可以使用时间戳来让数据库更新列,而不是使用 laravel 来做到这一点dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
    • 只需要一年.. 我怎样才能添加月份日期和时间?
    • 我在 laravel 中将字段名称更改为 creatioTime。但它只需要一年,没有月日和时间
    • 您的列的数据类型是什么?如果是日期时间,则应插入完整的日期和时间。您还可以查看 stackoverflow.com/questions/24404474/… 以了解将 created_at 更改为其他名称时可能出现的问题
    猜你喜欢
    • 2010-12-30
    • 2021-12-21
    • 2018-10-01
    • 2015-09-08
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 2020-03-03
    • 2014-12-12
    相关资源
    最近更新 更多