How to automatically update the timestamp of parent model in Laravel
How to automatically update the timestamp of parent model
In this session, we are going to try to solve the "How to automatically update the timestamp of parent model" puzzle by using the Laravel.
You can auto-update the created_at and update_at columns of model when the child model is created or updated in Laravel. Here we have inbuilt method in Laravel which can be used to update the updated_at and created_at column of parent model, whenever we create or update the child model.
For example: We can use this method in the comment model which contains the primary key of the post table as foreign key (post_id). Whenever we will make any comment on a particular post, it will update the created_at and updated_at column of that post.
app\Models\Comment.php
class Comment extends Model
{
use SoftDeletes;
protected $touches = ['post'];
public function post()
{
return $this->belongsTo('App\Models\Post');
}
-
How to define relationship in Laravel?
To define a relationship, we need first to define the post() method in Comment model. In the post() method, we need to implement the belongsTo() method that returns the result.
-
How many relationships are there in Laravel?
As you know, in relationships between tables in the database, we usually have 3 types of relationships. These relationships are called one-to-one, one-to-many, and many-to-many relationships.
-
What is with () in Laravel?
with() function is used to eager load in Laravel. Unless of using 2 or more separate queries to fetch data from the database , we can use it with() method after the first command. It provides a better user experience as we do not have to wait for a longer period of time in fetching data from the database.
-
What is difference between attach and sync in Laravel?
The attach function only adds records to the Pivot table. The sync function replaces the current records with the new records. This is very useful for updating a model.
-
What is use of latest() in Laravel?
latest() function in Laravel used to get latest records from database using default column created_at.
-
What is eager loading in Laravel?
Eager loading is super simple using Laravel and basically prevents you from encountering the N+1 problem with your data. This problem is caused by making N+1 queries to the database, where N is the number of items being fetched from the database.
-
Which is better lazy loading or eager loading?
Eager Loading. Eager loading generates all web page content as soon as possible, while lazy loading delays the display of non-essential content.
-
Does lazy load improve speed?
Lazy-loading images and video reduces initial page load time, initial page weight, and system resource usage, all of which have positive impacts on performance.
-
How to update parent model timestamp on create or update child model?
In Laravel a model that has a belongs to relationship to another, also has a
_id field on its table that points to the id of the parent. When that model is being associated to another the updated_at timespamp naturally gets updated, as the value of the _id field changes -
How to store timestamps() in Laravel?
By default, those pivot tables do not contain timestamps. And Laravel does not try to fill in created _at/updated _at in this situation. You can save the timestamps automatically all you need to do is to add them into migration file, and then define relationship using ->withTimestamps();
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- How to create pivot table in laravel using migration
- Best Practices for Error Handling in Production Server Code (example code)
- Skip first n record and display rest records in laravel view
- Show old value while editing the form in Laravel
- How to use bootstrap pagination in laravel 8
- Get duplicate records in laravel
- Credit card validation in laravel
- Update email with unique validation in laravel
- Array to string conversion laravel Controller
- Drop foreign key column in Laravel using migration
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- Print query in laravel
- How to randomly get the user id from users table in laravel
- Get comma separated email from input array
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- Update record after find method in lavavel
- JQuery each loop on json response after ajax in laravel
- Target class [admin] does not exist.
- Laravel file size validation not working
- How to send ID to another page in Laravel
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Send post data from controller to view
- If condition in Laravel 9
- How to get last month records in Laravel