Disable Laravel's Eloquent timestamps - php

I'm in the process of converting one of our web applications from CodeIgniter to Laravel. However at this moment we don't want to add the updated_at / created_at fields to all of our tables as we have a logging class that does all this in more depth for us already.
I'm aware I can set $timestamps = false; in:
Vendor\laravel\framework\src\illuminate\Datebase\Eloquent\Model.php
However I'd rather not change a core file for Laravel, or have everyone of my models have that at the top. Is there any way to disable this elsewhere for all models?

You either have to declare public $timestamps = false; in every model, or create a BaseModel, define it there, and have all your models extend it instead of eloquent. Just bare in mind pivot tables MUST have timestamps if you're using Eloquent.
Update: Note that timestamps are no longer REQUIRED in pivot tables after Laravel v3.
Update: You can also disable timestamps by removing $table->timestamps() from your migration.

Simply place this line in your Model:
public $timestamps = false;
And that's it!
Example:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public $timestamps = false;
//
}
To disable timestamps for one operation (e.g. in a controller):
$post->content = 'Your content';
$post->timestamps = false; // Will not modify the timestamps on save
$post->save();
To disable timestamps for all of your Models, create a new BaseModel file:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BaseModel extends Model
{
public $timestamps = false;
//
}
Then extend each one of your Models with the BaseModel, like so:
<?php
namespace App;
class Post extends BaseModel
{
//
}

If you are using 5.5.x:
const UPDATED_AT = null;
And for 'created_at' field, you can use:
const CREATED_AT = null;
Make sure you are on the newest version.
(This was broken in Laravel 5.5.0 and fixed again in 5.5.5).

If you only need to only to disable updating updated_at just add this method to your model.
public function setUpdatedAtAttribute($value)
{
// to Disable updated_at
}
This will override the parent setUpdatedAtAttribute() method. created_at will work as usual.
Same way you can write a method to disable updating created_at only.

In case you want to remove timestamps from existing model, as mentioned before, place this in your Model:
public $timestamps = false;
Also create a migration with following code in the up() method and run it:
Schema::table('your_model_table', function (Blueprint $table) {
$table->dropTimestamps();
});
You can use $table->timestamps() in your down() method to allow rolling back.

Eloquent Model:
class User extends Model
{
protected $table = 'users';
public $timestamps = false;
}
Or Simply try this
$users = new Users();
$users->timestamps = false;
$users->name = 'John Doe';
$users->email = 'johndoe#example.com';
$users->save();

Add this line into your model:
Overwrite existing variable $timestamps true to false
/**
* Indicates if the model should be timestamped.
*
* #var bool
*/
public $timestamps = false;

just declare the public timestamps variable in your Model to false and everything will work great.
public $timestamps = false;

Override the functions setUpdatedAt() and getUpdatedAtColumn() in your model
public function setUpdatedAt($value)
{
//Do-nothing
}
public function getUpdatedAtColumn()
{
//Do-nothing
}

You can temporarily disable timestamps
$timestamps = $user->timestamps;
$user->timestamps=false; // avoid view updating the timestamp
$user->last_logged_in_at = now();
$user->save();
$user->timestamps=$timestamps; // restore timestamps

Related

Laravel 6: User models uses UUID instead of auto-increment. Where to update DB session logic?

OK so my User models uses webpatser/laravel-uuid. All migrations are using UUID.
So now my model looks like:
<?php
namespace App\Models;
use App\Models\Traits\Uuid;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
class User extends Authenticatable
{
use Notifiable;
use Uuid;
public $incrementing = false;
public $timestamps = true;
protected $guarded = [
'uuid',
];
protected $keyType = 'string';
protected $primaryKey = 'uuid';
protected $table = 'users';
protected $dates = [
'created_at',
'updated_at',
];
protected $hidden = [
'password',
'remember_token',
];
public function setPasswordAttribute($password): void
{
$this->attributes['password'] = Hash::make($password);
}
}
I want to use database session driver. I created session table via php artisan session:table. All migrations are done. I obviously had to rename existing user_id column. I've changed it to user_uuid. I know it's not enough as I can't find the logic responsible for populating this db table. I guess it's somewhere in the vendor (Illuminate).
Where is the logic to populate my non-default session column?
Now each open the page gives:
So I know what's the issue, what's causing it, how to change it, but I don't know where to start. Thanks for any hints.
I think you would benefit of a custom session handler because the name of the column user_id is hardcoded into the addUserInformation() method.
Extend the existing DatabaseSessionHandler.php and replace the addUserInformation() method so it looks for the correct column name:
class DatabaseUuidSessionHandler extends DatabaseSessionHandler
{
protected function addUserInformation(&$payload)
{
if ($this->container->bound(Guard::class)) {
$payload['user_uuid'] = $this->userId();
}
return $this;
}
}
Register it in one of your service providers:
class SessionServiceProvider extends ServiceProvider
{
public function boot()
{
Session::extend('databaseUuid', function ($app) {
return new DatabaseUuidSessionHandler;
});
}
}
Finally update SESSION_DRIVER in your .env to use the newly created databaseUuid driver.
Remember that this is untested code and should only be used as a guideline of how this could work.

Laravel 6 Eloquent model serialization ignoring Accessors

I'm using Laravel 6 with a SQL Server 2017 database backend. In the database I have a table called PersonPhoto, with a Photo column and a Thumbnail column where the photos and thumbnails are stored as VARBINARY.
I have defined the following Eloquent model, with two Accessors to convert the images to base64 encoding:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PersonPhoto extends Model
{
protected $connection = 'mydb';
protected $table = 'PersonPhoto';
protected $primaryKey ='PersonID';
public function person(){
return $this->belongsTo('App\Person', 'PersonID');
}
public function getPhotoAttribute($value){
return base64_encode($value);
}
public function getThumbnailAttribute($value){
return base64_encode($value);
}
}
This works fine in Blade templates, however when I try to serialize to JSON or an Array I get a "Malformed UTF-8 characters, possibly incorrectly encoded" error, as if the Accessors are being ignored and the raw data is being serialized. To workaround this, I have altered the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PersonPhoto extends Model
{
protected $connection = 'mydb';
protected $table = 'PersonPhoto';
protected $primaryKey ='PersonID';
//Added to hide from and add fields to serializer
protected $hidden = ['Photo', 'Thumbnail'];
protected $appends = ['encoded_photo', 'encoded_thumbnail'];
public function person(){
return $this->belongsTo('App\Person', 'PersonID');
}
public function getPhotoAttribute($value){
return base64_encode($value);
}
public function getThumbnailAttribute($value){
return base64_encode($value);
}
//Added these new accessors
public function getEncodedPhotoAttribute(){
return base64_encode($this->Photo);
}
public function getEncodedThumbnailAttribute(){
return base64_encode($this->Thumbnail);
}
}
This hides the original Photo and Thumbnail fields from the serializer and includes the two new accessors. This appears to work and solves my issue.
Questions:
1) Is Laravel's serializer ignoring my Accessors as I suspect, and is this by design?
2) Although my workaround works, is this a reasonable approach or am I likely to run into problems? Is there a better way of doing it?
Thanks
I think you have two issues:
First, Laravel serialization requires that you append any accessors you want included — even if an attribute of the same name already exists. You did not explicitly append the desired values in the first example.
https://laravel.com/docs/5.8/eloquent-serialization#appending-values-to-json
Second, Laravel doesn't always like capitalized attribute names. It happily expects everything to be lowercase (snake_case) and based on some quick testing, seems to have some trouble associating a proper $value to pass to an accessor when case is involved.
However, you can modify your accessor to call the attribute directly instead of relying on Laravel to figure out what you are asking for and achieve the desired results.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PersonPhoto extends Model
{
protected $connection = 'mydb';
protected $table = 'PersonPhoto';
protected $primaryKey = 'PersonID';
// add the desired appends for serialization
protected $appends = ['Photo','Thumbnail'];
public function person()
{
return $this->belongsTo('App\Person', 'PersonID');
}
public function getPhotoAttribute()
{
// access the attribute directly
return base64_encode($this->attributes['Photo']);
}
public function getThumbnailAttribute()
{
// access the attribute directly
return base64_encode($this->attributes['Thumbnail']);
}
}
EDIT: I actually see that you did something similar in your second example with $this->Thumbnail and $this->Photo. My example is of the same concept, but without relying on magic methods.
__get/__set/__call performance questions with PHP

Laravel 5.7 - Eager loading with morphMany relationship and custom attribute getter

So I have the following models:
class TemplateEntity extends Model {
protected $table = "TemplateEntities";
const UPDATED_AT = null;
const CREATED_AT = null;
public function element() {
return $this->morphTo("element", "entity_type", "id_Entity");
}
public function getEntityTypeAttribute($entity_type) {
return 'App\\' . $entity_type;
}
}
class Template extends Model {
protected $table = "Template";
const UPDATED_AT = null;
const CREATED_AT = null;
public function entities() {
return $this->hasMany("App\TemplateEntity", "id_Template");
}
}
class TemplateEntity extends Model {
protected $table = "TemplateEntities";
const UPDATED_AT = null;
const CREATED_AT = null;
public function element() {
return $this->morphTo("element", "entity_type", "id_Entity");
}
public function getEntityTypeAttribute($entity_type) {
return 'App\\' . $entity_type;
}
}
I want to eager load template entity elements using Eloquent ORM's ::with() method, however whenever I do this I get an error:
//$template_id is defined as a controller param
$template = Template::with("entities", "entities.element")->where("id", "=", $template_id)->get()
"Class 'App\' not found"
I did some debugging and when I echo $entity_type in TemplateEntity's GetEntityTypeAttribute() method I get an empty value. However, my models generally work fine if I don't use eager loading, but I would like to add it to my application if possible to make it more efficient.
Any help you all can provide would help!
edit: fixed a typo, should have been Template::with instead of $template::with
Part of the problem might be a blank class in that variable. Suggest you use the class name when calling get(). So \App\Template:: instead of $template::.
Another item to help may be the way you are calling the relationship's eager load. Perhaps try to call through the function. This might work better for you:
\App\Template::with(['entities' => function($query){
$query->with('element');
}])->get();
The accessor function might be interfering with the Laravel morph function. I realise you want to use the shortened name of the class in the DB. To do this without the use of the getter (and globally), I suggest using a morphMap.
In AppServiceProvider inside the boot() method:
\Illuminate\Database\Eloquent\Relations\Relation::morphMap([
'MyTemplate' => \App\MyTemplate::class,
'Section' => \App\Section::class,
// etc.
]);
This will allow you to add only 'Section' to the DB and remove the accessor function from your class.

using touch() to update timestamp of custom timestamp field in laravel

Is there a way to use touch() for updating timestamp of is_online field in table instead of updating created_at field in laravel Eloquent ORM
At present I am using
User::where('id',$senderId )->update(array('is_online' => date('Y-m-d H:i:s')));
No, the touch method isn't written for updating anything other than the built in timestamps, but you could write your own function in your User Model, if you want to. Something like this
class User extends Eloquent implements UserInterface, RemindableInterface {
public function touchOnline()
{
$this->is_online = $this->freshTimestamp();
return $this->save();
}
}
and then do replace your old code with
User::find($senderId)->touchOnline();
A few more lines of code, but maybe a slight bit more readable.
You can find the code behind the touch function here, if you're curious.
Laravel 4.2
class User extends Eloquent implements UserInterface, RemindableInterface
{
public static function boot()
{
parent::boot();
/*
static::creating(function($table) {
$table->foo = 'Bar';
});
*/
static::updating(function($table) {
$table->is_online = $this->freshTimestamp();
// $table->something_else = 'The thing';
});
}
}
Usage. Just call the native touch method.
User::find($senderId)->touch();
A quick alternative is to override the CREATED_AT constant in your model like
Class User extends Model
{
protected UPDATED_AT = 'is_online';
}
$user->touch();
Just continue touching

Special UUID primary key for laravel eloquent

I have a users table that doesn't use an auto increment primary key, instead it uses a binary uuid primary key. I set up my custom model to interact with my table however, I'm having trouble trying to find records by using ::find() because for a case like this, this uuid needs to searched by using HEX() and UNHEX() mysql functions. How to I override this and have whatever is in ::find() to be hexed. The name of the model is Player.
So if I was to try to find a user with a uuid of 9d823b9eec224cbea10b69bec2c5adef, I cannot find them by doing:
Player::find('9d823b9eec224cbea10b69bec2c5adef') since the uuid needs to be unhexed.
I've tried Player::find("UNHEX('9d823b9eec224cbea10b69bec2c5adef')"); with no results.
Here's my model so far:
class Player extends Eloquent {
protected $table = 'players';
protected $connection = 'game';
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
}
The datatype for uuid is binary(16)
Update
I've got it to work by using Player::find(DB::raw("UNHEX('9d823b9eec224cbea10b69bec2c5adef')")); however this is a lot to write every time I need to look up a user.
Is there any way I can have the parameter for ::find() always run through DB::raw("UNHEX('uuid')") or be passed through the function hex2bin()?
I am 100% certain I will always be using UUID so I want to override ::find(), not create a new method.
I would try to unhex it in PHP prior to passing it to mysql:
Player::find(hex2bin('9d823b9eec224cbea10b69bec2c5adef'));
You could add this method to your Player class:
public static function findUUID($uuid) {
return self::find(hex2bin($uuid));
}
Now any where in your project you can call it like:
$result = Player::findUUID('9d823b9eec224cbea10b69bec2c5adef');
If you do not want to declare it statically you can:
public function findUUID($uuid) {
return self::find(hex2bin($uuid));
}
and then reference it in your code with:
$result = new Player;
$result->findUUID('9d823b9eec224cbea10b69bec2c5adef');
This should allow you to override the native find() behavior and not have to use findUUID() instead:
protected $primaryKey = 'uuid';
public static function find($uuid)
{
return parent::find(hex2bin($uuid));
}
If you really want it like that, you can also do Player::find('9d823b9eec224cbea10b69bec2c5adef') with this
class Player extends Eloquent {
protected $table = 'players';
protected $connection = 'game';
protected $primaryKey = 'uuid';
public $incrementing = false;
public $timestamps = false;
public static function find($uuid, $columns = array('*')) {
return self::find("UNHEX('$uuid')", $columns);
}
}
EDITED
added self, as user Elliot Fehr suggested

Categories