How to update model without updating created_at timestamp? - laravel

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.

Related

How to get specific columns from relation table in laravel?

I am trying to fetch soecific columns data from relational table but it is giving me null
$allConsignments = Consignment::query();
$allConsignments->select(['id','customer_reference'])->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
When I don't use select() then it gives correct data .
like this
$allConsignments = Consignment::query();
$allConsignments->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get()
it is working but I also need specific columns from Consignment Table. what could be the reason?
You can also do like this.
$allConsignments = Consignment::query();
$allConsignments::with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get(['id','customer_reference']);
Actually, I also need to select the foreign key column from the table on which relationship is based. for example in my case I have customer_id in consignment table so it should be like that
$allConsignments = Consignment::query();
$allConsignments->select('id','customer_reference','customer_id')->with('customers:name,id')->orderBy('id', 'desc')->limit(5000)->get();
I need to select customer_id as well

How to set default value to version column for existing data?

I am trying to use optimistic locking.
I am adding the version column to my table how do I set the default value to the version column for existing data or this is sufficient on entity?
#Version
#Column(name = "VERSION")
private Long version = 0L;
The most easiest way it to do this in the database.
Of course you need to add the version column anyway: something like:
alter table MyEntity add column version INT(11); //no not null constraint here!
and then just add the first value to all entities:
update MyEntity set 'version' = 1;
now you can also add the not null constraint
alter table MyEntity modify version INT(11) NOT NULL;
(I expect that you stop the application while you add the version column).
In case of Oracle as a database - use with values option for nullable columns
alter table MyEntity add column version INT(11) default 0 with values
for not-null columns - DB will updates to default value for existing rows
alter table MyEntity add column version INT(11) not null default 0
From Oracle-11g onwards, default values are retrieved from metadata
for null values on modified field, Oracle does not perform update on each row to fill default values.
see - https://chandlerdba.com/2014/10/30/adding-not-null-columns-with-default-values/

DB::insert - returns no error, yet no inserts result from statement

I have created a backup of a given table using the following raw queries:
DB::statement("DROP TABLE IF EXISTS answers_bup");
DB::statement("CREATE TABLE answers_bup AS TABLE answers");
The answers table has the following schema:
CREATE TABLE answers
(
id uuid NOT NULL,
user_id uuid NOT NULL,
survey_id uuid NOT NULL,
question_id uuid NOT NULL,
answer character varying(255) NOT NULL,
created_at timestamp(0) without time zone NOT NULL,
updated_at timestamp(0) without time zone NOT NULL,
}
Now, in order to restore a single row, from the answers_bup table to the answers table. I wrote the following DB::insert:
$void = DB::insert("INSERT INTO answers
SELECT
'?'::uuid AS id,
'?'::uuid AS user_id,
'?'::uuid AS survey_id,
'?'::uuid AS question_id,
answer,
created_at,
updated_at
FROM
answers_bup
WHERE
id='?'::uuid", [
$newId,
$user_id,
$survey_id,
$question_id,
$answer->id
]);
Basically, I only need to copy over three fields from the answers_bup table - answer, created_at and updated_at. The others, have to be assigned new values, hence the above statement.
When I run this code fragment, I get no errors. Yet, the insert does not happen. The answers table remains empty.
Could anyone help me understand what might be wrong here?
Try DB::statement(), or DB::table('answers')->insert('...')

laravel 5: soft delete

I added deleted_at filed in tables and the default value is 0000-00-00 00:00:00(which is must in mysql),now using User::first() can not get the value when deleted_at = 0000-00-00 00:00:00. any idea?
When you call the delete method on the model, the deleted_at column will be set to the current date and time. And, when querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results.
To make your code working set deleted_at field to null.
To delete the model you can use below logic
$deletedRows = App\Flight::where('active', 0)->delete();
Reference http://laravel.com/docs/5.1/eloquent#soft-deleting
How did you add the column?
If you use Laravels Schema within a migrations there is a softDeletes() column which defaults to null (you could also do this in your database directly of course).
You can only get the value of record has deleted_at = NULL otherwise row cannot be retrieved. If you want to get the desired row, just set the deleted_at = NULL
You should use softDelete like the code below, it will automatically create proper column with proper default value.
Schema::table('table_name', function(Blueprint $table)
{
$table->softDeletes();
});

Column that will dysplay current date in grocery_crud CodeIgniter

In grocery_crud and Code Igniter in table Employees I have column today(dd/mm/yyyy). How to set column to display current date. Please help me.
With MySQL you can't do this with a Date but with a Timestamp column, and you add 'CURRENT_TIMESTAMP' as the default value.
That said as of MySQL version 5.6.5 you can do it with :
CREATE TABLE foo (
`creation_time` DATETIME DEFAULT CURRENT_TIMESTAMP,
`modification_time` DATETIME ON UPDATE CURRENT_TIMESTAMP
)
as explained here :
https://stackoverflow.com/a/10603198/1226118

Resources