How to set the CURRENT_TIMESTAMP into certain column in timestamps function? - laravel-5

I am using Laravel 5.4, i read the docs said that, there are two function that use really similar name, it's timestamps and timestamp:
Timestamps
$table->timestamps();
Adds nullable created_at and updated_at TIMESTAMP equivalent columns.
Timestamp
$table->timestamp('name_column');
TIMESTAMP equivalent column.
As you can see, timestamps created a default two column, created_at and updated_at with default TIMESTAMP attribute. How could i make only one of the column, which updated_at have CURRENT_TIMESTAMP attribute which just using a single command ?
I have done like below:
$table->timestamps()->useCurrent();
But i got an error instead using above command, so instead i used default(\DB::raw('CURRENT_TIMESTAMP')). To solved this problem, i have to make two command, like below:
$table->timestamp('created_at');
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
How could i make that happen ?

Related

How to specify in supabase a field should only have default value?

I'm making a basic chat app using supabase.
I've figured out how to make it so users can only create/edit their profile and send messages from their profile using RLS, by checking if their id matches with the auth.id.
The problem I'm facing is, that I don't want users to be able to insert messages with wrong creation dates.
If no date is specified the default value is correct. But currently there is no check for the creation date.
Can I specify, that the creation date field has to be sent empty?
Is there maybe a better solution?
One thing you can do is you can override the created_at field with the current server timestamp using triggers.
Code not verified, so you might have to make some small adjustments, but the idea is this:
create extension if not exists moddatetime schema extensions;
-- assuming the table name is "messages", and a timestamp column "created_at"
-- this trigger will set the "created_at" column to the current timestamp for every insert
create trigger
handle_created_at before insert
on messages
for each row execute
procedure moddatetime(created_at);

Laravel automatically add "on update CURRENT_TIMESTAMP" to some certain columns

My migration is like so:
$table->bigIncrements('id');
$table->timestamp('from_date');
$table->timestamp('to_date');
$table->timestamps();
The problem is that when I migrate it, the second one which is from_date automatically gets on update CURRENT_TIMESTAMP attribute so it means when I update other columns this column will be updated too. That's what I don't want. How can I prevent it?
You can add nullable to the column in order to remove the constraint.
$table->timestamp('from_date')->nullable();
Unfortunately I think that this is the only solution. Then you can add a form validation in order to prevent setting null value for the field.
-- EDIT
$table->timestamp('from_date')->default(DB::raw('CURRENT_TIMESTAMP'));
// or
$table->timestamp('from_date')->useCurrent();
Try this as well, I believe this is what the created_at has.
You need to make the DateTime column nullable, then MySQL won't add that. By default, MySQL adds that to the first timestamp in the table, unless explicitly told not to (via allowing a null value for the field). This is a MySQL thing, not a Laravel thing.
$table->timestamp('colName')->nullable();
Read :
Automatic Initialization and Updating for TIMESTAMP and DATETIME - MYSQL DOCS
Laravel & MySQL auto-adding “on update current_timestamp()” to timestamp fields
This behaviour is native to MySQL. You can read about it on this documentation page:
TIMESTAMP and DATETIME columns have no automatic properties unless they are specified explicitly, with this exception: If the explicit_defaults_for_timestamp system variable is disabled, the first TIMESTAMP column has both DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP if neither is specified explicitly.
The docs go on to specify that there are two strategies for avoiding this behaviour:
Enable the explicit_defaults_for_timestamp variable in your database configuration.
When defining your timestamp column, specify a DEFAULT or make the column nullable.

Get latest row using Laravel?

It appear I am not getting latest row when rows have actually same created_at value.
Using $model->latest()->first() - I am getting first row rather than last row of created_at.
How to solve this?
latest() will use the created_at column by default.
If all of your created_at values are the exact same, this obviously won't work...
You can pass a column name to latest() to tell it to sort by that column instead.
You can try:
$model->latest('id')->first();
This assumes you have an incrementing id column.
This will entirely depend on what other data you have in your table. Any query of a relational database does not take the "physical position" into account - that is, there is no such thing as being able to get the "last inserted row" of a table until you are able check some value in the table that indicates it is the probably the last row.
One of the common ways to do this is to have a auto-incrementing unique key in the database (often the Primary Key), and you can simply get the largest value in that set. It's not guaranteed to be the last row inserted, but for most applications this is usually true.
What you need is the equivalent query to be executed
SELECT * FROM Table WHERE created_at = ? ORDER BY ID DESC LIMIT 1
or, in Eloquent ORM
$model->where('created_at', '=', ?)->orderBy('id', 'desc')->take(1)->first();
Keep in mind that you'll probably need other filters, since it is entirely possible other users or processes may insert records at the same time generating the same creation date, and you'll end up with somebody elses records.

How to create a column with specific timestamp?

create table my_table(
id NUMBER(5),
my_date TIMESTAMP(3) DEFAULT CURRENT_TIMESTAMP,
constraint customers_pk primary key (id)
);
I want to make the TIMESTAMP like this: 'YYYY-MM-DD HH24:MI:SS',
and what does the number in brackets of timestamp mean?
timestamps don't have a format.
They are stored in a binary representation. If you want to display the values in a specific format, use to_char() to format it as a string or do that in your application.
as far as I know, you cannot change its format on CREATE TABLE.
on querying if you use proper NLS Format, you wont deal with this again and again

Add constraint to already existing column to store Record Created Timestamp

I have a Existing column called CREATED DATE whose data type is "Date". I am trying to add a constraint to this CREATED DATE Column which will store "Record created time stamp". I have a following query in place but its working out. Any suggestions will be helpful.
ALTER TABLE CONTAINER_SCAN_LOG
MODIFY (CREATED_DATE DEFAULT (sysdate());
what is the error exactly ? what is the default date? anyway try to remove the parenthesis
ALTER TABLE ex_employee
MODIFY START_TIME DEFAULT (sysdate);
use timestamp instead of date
alter table t add (created_datetime timestamp default systimestamp)
or modify existing created_date to be of timestamp datatype and systimestamp default

Resources