Laravel - custom timestamp column names - laravel

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

Related

Alter custom ID column name Laravel 7

We are transforming an old project to Laravel 7
the users table doesn't follow laravel naming
the id column is named "UserId" and the problem it is not auto increment
I already set in the User model
protected $primaryKey = 'UserId';
how can I alter the column to be Auto-Increment, keep in mind it is used in foreign constraints, thank you
If the name of the user table is different, you should specify this naming in the model section. Also, why not change the related column type of the migration process to include auto-increment. To set the auto-increment property, in the model part protected $incrementing = true; You can specify.

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');

How to change date format of timestamps on pivot in Laravel?

I'm trying to change the date type of timestamps (created_at, updated_at, deleted_at) globally in the whole application from timestams to timestampsTz. In migrations it's no problem. In single models either, since I can overwrite the $dateFormat parameter of the model.
But I have problem with pivot tables containing timestamps, because they don't inherit any parameters from my models.
Let's say I have a products and attributes table with attribute_product pivot between them containing columns like value and timestamps. If I try to retrieve all attributes with values and timestamps of a product I would do something like this: $product->attributes but I get the error:
local.ERROR: Trailing data {"exception":"[object] (InvalidArgumentException(code: 0): Trailing data at \\vendor\\esbot\\carbon\\src\\Carbon\\Carbon.php:910)
[stacktrace]
#0 \\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php(716): Carbon\\Carbon::createFromFormat('Y-m-d H:i:s', '2019-06-25 16:1...')
#1 \\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php(739): Illuminate\\Database\\Eloquent\\Model->asDateTime('2019-06-25 16:1...')
#2 \\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Concerns\\HasAttributes.php(532): Illuminate\\Database\\Eloquent\\Model->fromDateTime('2019-06-25 16:1...')
#3 \\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(230): Illuminate\\Database\\Eloquent\\Model->setAttribute('created_at', '2019-06-25 16:1...')
#4 \\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Eloquent\\Model.php(248): Illuminate\\Database\\Eloquent\\Model->fill(Array)
...
The builder retrieves the timestamp in the correct format (e.g. "2019-06-25 16:17:01+02") but when it tryes to hydrate the related models pivot data, it uses the wrong format "Y-m-d H:i:s" and fails.
Is there any proper way to achieve this? Or is there any usable workaround?
Any help appreciated? Thanks in advance!
Have you tried creating an intermediary model for your pivot tables? You should then be able to alter the dateFormat property.
For example, you can create a pivot model like so:
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class RoleUser extends Pivot
{
}
And update the relationship on the other model to use that Pivot model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany('App\User')->using('App\RoleUser');
}
}
Then because you have a class for the Pivot model you should be able to specify the format.
https://laravel.com/docs/5.8/eloquent-relationships#defining-custom-intermediate-table-models

Laravel sqlsrv - unable to create timestamps()

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 ;) )

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