0

In my group page i am creating new group ID, when i create the groupID automatically that time need to store into database, column creationTime. I am using laravel 5.2 framework for building my application. How can i take that creation time and store the same to the database when inserting groupID to the table. my 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 is

@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',
    ];
}

Can anyone please tell how to do , replies are appreciable.

Rahul Vp
  • 127
  • 2
  • 8
  • 19

3 Answers3

3

Use

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

in your migration and Laravel will include inserted_at and updated_at for you and it will also take care of updating this columns for you when you use the model to insert data. You will not need to add any code to get the dates set.

Also you have a Group model, but you are not using it in your controller. Instead of DB::table('devicegroup')->insert($data); you could use Group::create($data); and similar for getting data.

rypskar
  • 2,012
  • 13
  • 13
  • how to change the name of field from create_at to creationTime? – Rahul Vp Jul 28 '16 at 11:18
  • The Model you are extending have `const CREATED_AT = 'created_at';`, you could try to change it there. Or if you have control of the database you could use timestamp to get the database to update the column instead of using laravel to do it https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html – rypskar Jul 28 '16 at 11:29
  • its taking year only.. how can i add month day and time? – Rahul Vp Jul 28 '16 at 11:40
  • i changed field name to creatioTime, in laravel . But it taking only year, no month day and time – Rahul Vp Jul 28 '16 at 11:41
  • What is the datatype of your column? If it is datetime the full date and time should be inserted. You could also have a look at http://stackoverflow.com/questions/24404474/change-name-of-laravels-created-at-and-updated-at for possible problems with changing created_at to other name – rypskar Jul 28 '16 at 11:44
  • Another problem is ,when i use Group::create($data) code, accountID is not inserting to the database. – Rahul Vp Jul 28 '16 at 11:58
  • accountID is not in the `$fillable` array, you need to add everything you want to set from create there – rypskar Jul 28 '16 at 12:07
  • how can i add month day , time?. Its inserting creation year only – Rahul Vp Jul 29 '16 at 08:07
1

Select Mysql Date type Timestamp Desfault value CURRENT_TIMESTAMP

example:

ALTER TABLE `tbl_name` ADD `cdate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
Basant Rules
  • 785
  • 11
  • 8
0

if use model work is very simple and create and update time updated automatic.

1-create model with name Group and extend model 2-add to filed created_at and updated_at in database devicegroup table 3-for add new data to this use below code :

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

and created_at filed automaticly inserted.

Saman
  • 2,506
  • 4
  • 22
  • 41
  • yeah am using Model. i ll add model in my question. But i didn't understand , where could i use this code? – Rahul Vp Jul 28 '16 at 10:10
  • instead `$ck=DB::table('devicegroup')->insert($data);` use top code to insert to table and created_at automatic update. – Saman Jul 28 '16 at 10:19
  • here where is the code for inserting creationTime?. Actually am new in laravel – Rahul Vp Jul 28 '16 at 10:48
  • can you tell me how to take current dateTime value to below X in laravel 5.2.. "creationTime"=> $postGeozone['X'] – Rahul Vp Jul 28 '16 at 10:52