Timestamps in laravel is not working well - laravel-5

What happen to my created_at and updated_at fields? Im using laravel5.1. I have this on my table:
=====================================
Name type default
created_at | timestamp | 0000-00-00 00:00:00
updated_at | timestamp | 0000-00-00 00:00:00
Migration is this:
$table->increments('id')->unsigned();
$table->timestamps();
Result The red is my actual date, the first green is the created_at and the second green is the updated_at

Related

Finding a conditional min date on tableau

I have a database with lots of records that have a date
I want to show the minimum date where there is no different booking type after that date
With these fields
Date, Booking type, Id
ID | Date | Booking type
123 01/04 A
123 01/05 B
123 01/06 A
123 01/07 A
So I would only want to show record on date 01/06 for ID 123 booking type 'A' as there has not been a different booking type after that
At the moment I can only get date 01/04 for booking type A id 123
First you calculate how many distinct Booking Type there is for each ID and Date
{FIXED [ID], [Date]: COUNTD([Booking Type])}
Once done, you can do a filter where SUM(....) > 1

How to set null in updated_at` field?

I laravel 5.7 app I see that for some rows
field defined as :
updated_at TIMESTAMP NULL DEFAULT NULL,
has value 0000-00-00 00:00:00
I try to run next statement :
DB::table('clients')
->where('updated_at','0000-00-00 00:00:00')
->update([
"updated_at" => null
]);
But got error :
: Invalid datetime format: 1292 Incorrect datetime value: '0000-00-00 00:00:00' for column 'updated_at' at row 1 (SQL: update `clients` set `updated_at` = whe
re `updated_at` = 0000-00-00 00:00:00)
Which is correct ?
Thanks!

How to add unique key for 2 columns : employee_id and only month and year from the date column

I have a table where I have all the employee details. I want to make employee_id and only month and year from date_of_salary as unique on the table. Only one row should be present for that particular employee ID and month/year. The date_of_salary is a column with date datatype.
I tried using the following but this works for the employee id and complete date. It takes multiple entries for same month.
$table->unique(['employee_id', 'date_of_salary']);
This is my complete up function for the migration
public function up()
{
Schema::create('employee_salary_details', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('employee_id')->required();
$table->date('date_of_salary')->required();
$table->foreign('employee_id')
->references('id')
->on('employees')
->onDelete('restrict');
$table->unique(['employee_id', 'date_of_salary']);
$table->timestamps();
});
}
Each row should be unique for an employee and salary month/year. Because the salary can happen only once a month but on any date.
->unique() means for example date_of_salary column has unique dates. So you can not add multiple times '2018-12-21', just once in the whole column.
I think you need ->primary(['employee_id', 'date_of_salary']), if you want unique rows by multiple columns.
| employee_id | date_of_salary |
+-------------+----------------|
| 1 | 2018-11-21 | <- OK
| 1 | 2018-12-21 | <- OK
| 2 | 2018-12-21 | <- OK
| 1 | 2018-12-21 | <- Not possible
Created 3 different fields in database for day, month and year. Made the employee_id, month and year as unique key with 3 column names.

How to update model without updating created_at timestamp?

Laravel has two timestamps on every table(created_at and updated_at).
I thought it would insert created_at at insertion of new row and update updated_at at update. However what happens is that created_at field is getting an update when I update my model.
How do I alter this behavior or what method should I use to update a row and just updated_at timestamp?
Right now I use $model->save();
$point = Map::find($id);
$point->longitude = $request->longitude;
$point->latitude = $request->latitude;
$point->description = $request->description;
$point->save();
I found a solution to my problem.
I created created_at and updated_at columns manually and created_at had on Update assign current_timestamp attribute.

DataMapper naming convention clashes with existing MySQL table

I am working on a MySQL table that is used by another program. What I want to do is to put up a web interface for this database with Sinatra and DataMapper. However, when I declare my property in the DataMapper model, I run into some problem with naming convention.
For example, the field in MySQL table is ControlStationID, and I declared as such, but when DataMapper runs, it change it to control_station_id. Anyway I can rectify this? I can't change the table structure.
Thanks.
Error seen:
DataObjects::SQLError: Unknown column 'control_station_id' in 'field list' (code: 1054, sql state: 42S22, query: SELECT `id`, `control_station_id` FROM `returnmessage` ORDER BY `id`)
MySQL table structure
mysql> show fields from returnmessage;
+-------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------+------+-----+---------+----------------+
| ID | bigint(20) | NO | PRI | NULL | auto_increment |
| ControlStationID | int(11) | YES | | NULL | |
+-------------------+---------------+------+-----+---------+----------------+
My code
class ReturnMessage
include DataMapper::Resource
property :ID, Serial
property :ControlStationID, Integer
end
repository(:default).adapter.resource_naming_convention = lambda do |value|
value.downcase
end
Solution:
property :ControlStationID, Serial, :field=>'ControlStationID'

Resources