Laravel sqlsrv - unable to create timestamps() - laravel

I am migrating an existing projekt in L5.1.x to a new server running with Server 2012 and an SQL Server 2012.
I have some problems when using the timestamps() fields for tables. When ill add in my Scheme:
$table->timestamps();
Ill get the created_at and updated_at column with type "datetime".
But with that new configuration when ill try to add a Model to this database i am always getting that error:
QueryException: Unable to convert an nvarchar Value to and datetime Value ....
When ill disable the timestamp fields in my Model, everything is working:
public $timestamps = false;
.. but of course no values for created_at and updated_at
I dont know what cause this problem - any ideas? i could manually set the created_at and updated_at field to "GETDATE()" - but i would like to use Laravels base functionality.

Ok ill found the problem - Laravel creates a datetime with format of:
"Y-m-d H:i:s.u"
But the SQL Server wants the format of:
"Y-d-m H:i:s"
So when you update your model function with:
public function fromDateTime($value)
{
return Carbon::parse(parent::fromDateTime($value))->format('Y-d-m H:i:s');
}
You are able to convert to date format to the correct value for your SQL Server ( dont know why its Y-d-m, its not my server ;) )

Related

Wrong date fetch with eloquentt from MongoDB

I get wrong date from MongoDB using Eloquent in Laravel. My record in database looks like this
"created_at" : ISODate("2020-11-17T15:30:42.131+01:00")
Code to get records from MongoDB
$taskObj = TaskComments::where('task_id', $task_id)->get()->toArray();
Result date for created_at is
1970-01-25 20:31:23 which is wrong, I would like to get in this format 2020-11-17 15:30:42
I would like to create correct Mutator or to define a default date format in Laravel or in MongoDb I am not sure.
Any help is appreciated.
This is fixed my problem, I used a mutator in Model of TaskComments
public $timestamps = FALSE;
public function getCreatedAtAttribute($created_at){
return $created_at->toDateTime()->format('Y-m-d H:i:s');
}

Insert timestamp type data into Oracle database through codeignier

I am trying to insert a row into Oracle though Codeigniter Active record. Basically I am getting an error for created_date column whose datatype is timestamp.
My code is like
$data['CREATED_DATE'] = getDatetimeforDB();
$this->db->insert("MY_TABLE_NAME", $data);
function getDatetimeforDB() {
return date("j-M-y h.i.s.u A");
}
But after executing the code, I am getting datetime error :
ORA-01830: date format picture ends before converting entire input string - Invalid query: INSERT INTO "MY_TABLE_NAME" ("ACTIVITY", "ACTIVITY_LOG", "CREATED_DATE", "USER_ID") VALUES ('users/logout', '"Logout"', '10-Aug-20 12.46.02.000000 PM', '555')
Please let me know what may be the issue. If I run the query directly through sql query, it is working fine, but not working through codeigniter.
Thanks in advance.
Used this PHP date() function as date("Y-m-d H:i:s")
$data['CREATED_DATE'] = date("Y-m-d H:i:s");
$this->db->insert("MY_TABLE_NAME", $data);

What can _time suffix use for laravel database design?

Most of us are used to the field names like create_time, update_time. Now laravel changed that into created_at, updated_at. Therefore the _time suffix are available for other things.
I, am self, take naming very seriously. And I don't want to waste any good word for naming. What can _time suffix use for laravel database design?
you can override the default laravel created_at and updated_at fields.
class MyModel extends Model
{
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
}
and also when creating the database instead of using laravel default $table->timestamps(); you can specify the names of the created_at and updated_at columns names as your wish.
$table->timestamp('create_time');
$table->timestamp('update_time');

Laravel - custom timestamp column names

I am migrating a site from codeigniter to Laravel.
For a legacy table reports, some existing columns created_at and updated_at are named date_created and date_modified respectively.
I wish to tell my eloquent Report model about these custom timestamp column names.
The documentation only provide reference to turning timestamps off or providing custom timestamp formats.
http://laravel.com/docs/eloquent#timestamps
In model your can define constants like this to change the column names
class BaseModel extends Eloquent {
const CREATED_AT = 'date_created';
const UPDATED_AT = 'date_modified';
}
or use you can use something like this Managing Timestamps

updated_at timestamp not being updated when using sync in Laravel 4

I use the following code to update tags and other information about an organization:
Route::put('org/{org}', function(Org $org){
$org->description = Input::get('description');
$org->website = Input::get('website');
$org->save();
$org->tags()->sync(Input::get('tags'));
return Redirect::to('org/'.$org->id)
->with('message', 'Seccessfully updated page!');
});
However, if I only change the tags associated with this org, the updated_at field is not updated. I added protected $touches = array('org'); to my Tag model, but this only seems to work for belongsTo relations, while the relation between orgs and tags is a many-to-many polymorphic relation.
Is there a way to allow the sync function to automatically update the default updated_at timestamp?
Doing it manually, like below, will update the timestamp even if my edit doesn't actually change anything:
$org->updated_at = \Carbon\Carbon::now()->toDateTimeString();
$org->save();
You have to setup touches array on Tag model, BUT it has limitation:
Eloquent guesses the relation name, so will work only if the name complies with the convention.
Organization model -> organizations relation.
And if you want to manually update the timestamp you can use this instead:
$model->touch();

Resources