How to disable or remove updated_At and Created_at when returning data

laravel remove created_at and updated_at
disable updated at laravel
laravel created at and updated at not working
laravel hidden timestamp
laravel model attributes

I'm trying to remove created_at and updated_at before returning response to api . I want to removie these field from both Place_Type and Places how can I do that? : I tried : unset(placetypes) but it didn't work

This is my Code:

    public function places()
    {

        $placeType = PlaceType::with('places')->where('id', 1)->get();

        return response()->json(['placeType' => $placeType]);
    }

The request result:

 "placeType": [
        {
            "id": 1,
            "name": "Moriah O'Conner",
            "icon": "https://picsum.photos/200/300",
            "status": 1,
            "created_at": "2019-12-14 18:23:19",
            "updated_at": "2019-12-14 18:23:19",
            "places": [
                {
                    "id": 1,
                    "name": "Linda Leffler",
                    "description": "Alice's shoulder, and it set to work, and very soon came to ME, and told me he was in the air. She did it so VERY remarkable in that; nor did Alice think it would feel very queer to ME.' 'You!' said.",
                    "icon": "https://picsum.photos/200/300",
                    "image_name": "https://picsum.photos/200/300",
                    "rating": 2,
                    "longitude": -53.389979,
                    "latitude": 19.633458,
                    "availability": 1,
                    "status": 1,
                    "place_type_id": 1,
                    "created_at": "2019-12-14 18:23:19",
                    "updated_at": "2019-12-14 18:23:19"
                },
                {
                    "id": 2,
                    "name": "Lauren Cartwright",
                    "description": "I should say \"With what porpoise?\"' 'Don't you mean by that?' said the King. 'I can't remember half of anger, and tried to look at it!' This speech caused a remarkable sensation among the leaves.",
                    "icon": "https://picsum.photos/200/300",
                    "image_name": "https://picsum.photos/200/300",
                    "rating": 1,
                    "longitude": -38.117034,
                    "latitude": -32.248637,
                    "availability": 1,
                    "status": 1,
                    "place_type_id": 1,
                    "created_at": "2019-12-14 18:23:19",
                    "updated_at": "2019-12-14 18:23:19"
                }...,
               }

There are different methods you can use.

Method 1 : Fetch only required fields from the database

You can use select() method for retrieving only required fields from db. Hence you can omit the unnecessary fields.

$placeType = PlaceType::with(['places'  => function ($query) {
                    $query->select('id', 'name', 'description', 'icon',
                        'image_name', 'rating', 'longitude', 'latitude',
                        'availability', 'status', 'place_type_id'); //timestamps excluded
                }])
                ->select('id', 'name', 'icon', 'status') //timestamps excluded
                ->where('id', 1)
                ->get();

return response()->json(['placeType' => $placeType]);

This code will output only specified fields both in the parent model (placetype) and child model (places).

If you use these customized select query more than once and writing all field names multiple time is difficult, then you could use model scope like the following.

PlaceType Model

// add all columns from your table
protected $columns = ['id', 'name', 'icon', 'status', 'created_at', 'updated_at'];

public function scopeExclude($query,$value=[]) 
{
    return $query->select( array_diff( $this->columns,(array) $value) );
}

Place Model

// add all columns from your table
protected $columns = ['id', 'name', 'description', 'icon', 'image_name',
                        'rating', 'longitude', 'latitude', 'availability',
                        'status', 'place_type_id', 'created_at', 'updated_at'
                    ];

public function scopeExclude($query,$value=[]) 
{
    return $query->select( array_diff( $this->columns,(array) $value) );
}

Then you could remove unwanted fields like the following

$placeType = PlaceType::with(['places' => function ($query) {
                    $query->exclude(['created_at', 'updated_at']); //exclude fields from Place model
                }])
                ->exclude(['created_at', 'updated_at']) //exclude fields from PlaceType model
                ->where('id', 1)
                ->get();

Courtesy : This SO answer by @Razor

Method 2 : Hide your column from serialization where you need

You can hide your column from serialization using laravel's makeHidden() method. In this method after fetching rows with all fields, you are making the specified fields as hidden. [Please note that the excluded variables won't appear on json but may visible on dump].

//get rows with all fileds (except hidden)
$placeType = PlaceType::with('places')->where('id', 1)->get();
//making timestamps hidden in child model's rows
$placeType->places->makeHidden(['created_at','updated_at']);
//making timestamps hidden in parent model's rows
$placeType->makeHidden(['created_at','updated_at']);

return response()->json($placeType);

Courtesy : This SO answer by @sajed

Method 3 : Using Hidden property

If the timestamps are unnecessary in most of the time in the app, you could use the model's hidden property.

PlaceType Model & Place Model

protected $hidden = ['created_at', 'updated_at'];

Hope this will be helpful. 🤗

Laravel, Laravel would try to automatically fill in created_at/updated_at and wouldn't find them. To disable that automatic timestamps, in your Eloquent Model you need What if you're working with non-Laravel database and your timestamp columns are named differently? Change Timestamp Date/Time Format. But sometime you require to prevent set timestamp of created_at and updated_at column, Maybe you don't have. So in this post we will learn how to make disabled timestamps value from model. So, there are two way to create disable created_at and updated_at timestamps in laravel 5 application.

Add the fields to the $hidden array:

// Model

protected $hidden = ['created_at', 'updated_at'];

8 Tricks with Laravel Timestamps, For this particular table, I only require the created_at to be set. I haven $this-> model->usesTimestamps()) return $values; $column and make use of the static ::updating event for removing any updated_at content. "laravel folder"\vendor\ laravel\framework\src\Illuminate\Database\Eloquent\Model.php. The I/O and WAL generation of moving the smaller, required amount of data is much less than removing all the old data. Then you can just drop the old table. Then you can just drop the old table. And most people do this via a trigger-based method: have every write to the old table also write to the new one as well.

1) You just need to declare public $timestamps = false; in every model you want to hide it.

2) You can also disable timestamps by removing $table->timestamps() from your migration.

3) Declare protected $hidden = ['created_at', 'updated_at']; in your model.

How to stop eloquent trying to set updated_at, So I'm trying to get Laravel to work with our old ginormous MySQL 5 database created by multiple I'm seeing how to disable timestamp handling, but not how to customize it per table I would And if you want to change the format, use this example I found on google. public function getCreatedAtAttribute($date) { return� The materialized view is a powerful database solution that allow us to access the view’s data faster by “caching” its response. The materialized view query is executed once when the view is created, not when accessing the data as it is with regular database views.

In case you want to remove timestamps from 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_table', function (Blueprint $table) {
    $table->dropTimestamps();
});

You can use $table->timestamps() in your down() method to allow rolling back. or in model

const UPDATED_AT = null;
const CREATED_AT = null;

created_at but no updated_at column, laravel 5 timestamps disable, laravel disable updated_at, laravel only created_at, laravel update without timestamps, laravel timestamp So we have to stop add update those column, otherwise it returns error. use Illuminate\Database\ Eloquent\Model; How to delete directory in Laravel 7.x and 6.x? How do I remove the "Sent from Mailspring" text from sent emails? How are Office 365 "flagged" emails reflected in Mailspring? What data does Mailspring collect when I use the product? I'm receiving repeated / duplicate open and link tracking events - why? How do I print an email? Can I import mail from another email application?

Assuming that $placeType is array, you can use this recursive function:

function removeTimeStampValues($array) 
{
    if(array_key_exists('created_at', $array) && array_key_exists('updated_at', $array)) {
       unset($array['created_at']);
       unset($array['updated_at']);

    }
    foreach ($array as $key => $value) {
       if(is_array($value)) {
         $array[$key] = recursiveRemoveTimeStampValue($value);
       }
    }

    return $array;
}

Disable timestamps(created_at and updated_at) in Laravel 5 Model , When we try to use only UPDATED_AT, without CREATED_AT, https://github. com/laravel/framework/pull/21178/files -PR for disable UPDATED_AT @return Schema\Builder */ protected function schema() { return /framework/src/ Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 548� How to keep apps removed from Windows 10 from returning during an update. 05/25/2018; 3 minutes to read +6; In this article. Applies to: Windows 10 (Semi-Annual Channel) When you update a computer running Windows 10, version 1703 or 1709, you might see provisioned apps that you previously removed return post-update.

[Proposal] Allow to disable CREATED_AT � Issue #1071 � laravel , While it's very easy to create the migration without the updated_at column, Eloquent would still try and set that timestamp when creating records� Since you are planning to return it what I would personally do is go: Start Menu->Settings->Update & Security -> Recovery. Under where it says "Reset this PC" click "Get Started". When you do this it presents you with two options: Keep My Files, Remove Everything.

Disabling just created_at or updated_at in a Laravel model, Note that you will need to place updated_at and created_at columns on your table by default. When soft deleting a model, it is not actually removed from your database. Instead To enable soft deletes for a model, make it inherit from the SoftDeletes mixin: The return value of the relation is the class of the related model. Type the name of agent (e.g., Disable Return Receipt). Select "Manually from Actions Menu" in the "When should this agent run" field. Select "Selected documents" in the "Which document should it act on" field. Click the Add Action button below.

ORM | Documentation, Timestamp ), then a corresponding model will save value into this column only once data in this column, hence you can use it to investigate data change issues. You can simply turn off automatic management of these fields: return this; The sql_mode options needed to disable NO_ZERO_IN_DATE errors are: sql_mode = "ONLY_FULL_GROUP_BY,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION" Then, restart MySQL with sudo service mysql restart and check your sql_mode if desired.

Comments
  • This must work for any Model query. Please use protected $hidden = ['created_at', 'updated_at']; in both placeType and places model.
  • I did that but it still returns updated_at and created_at
  • Do it in both model Place_Type and Places @Developer
  • And if need them but don't want to show them set them in $hidden in your model. protected $hidden = ['created_at', 'updated_at']; @Developer
  • I did that but it still returns updated_at and created_at
  • protected $timestamps = false;
  • protected gave me an error ->Access level to App\\PlaceType::$timestamps must be public
  • $timestamps property is used to tell Laravel whether to add the timestamp updates to the SQL update/insert statements. Not to hide the properties from the JSON responses
  • just put this protected $hidden = ['created_at', 'updated_at'];