I have a small question about Laravel's pivot table and it's timestamps (created_at/updated_at).
The scenario is:
assign many tickets to many users, and see when it was assigned.
I want to keep the created_at
timestamp, but want to get rid of the updated_at
column when creating the migration.
In my research I've stumbled upon the solution to declare public $timestamps = false;
in the model. But a pivot table does not have a model - and it will disable both created_at
and updated_at
.
Other than this I haven't found any results that matches my case.
Can anyone enlighten me here? Thanks
Update
From Laravel 5 you can just fix this issue and maintain your time stamps in your pivot table using withTimestamps()
like that.
return $this->belongsToMany('App\Role')->withTimestamps();
https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
But a pivot table does not have a model
This is not true. From the Laravel docs:
If you would like to define a custom model to represent the intermediate table of your relationship, you may call the
using
method when defining the relationship. Custom many-to-many pivot models should extend theIlluminate\\Database\\Eloquent\\Relations\\Pivot
class while custom polymorphic many-to-many pivot models should extend theIlluminate\\Database\\Eloquent\\Relations\\MorphPivot
class.
To do this, just create a model for your pivot table and then the rest of the steps are pretty much the same as they would be for a usual model.
You can set const UPDATED_AT = null;
to only use created_at
. More details in this SO thread
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.