Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining table created_at and updated_at values not being set

I have two models, Product and Attribute. There is a 'product_attributes' joining table between 'products' and 'attributes'. The relationship is defined in both models using belongsToMany(), and the key of the table is a compound one of product_id and attribute_id.

I can successfully retrieve and store records in the joining table - so, as such everything is functioning as expected, with the exception that the created_at and updated_at in product_attributes is not being set.

Is the above by design, and is there anything I can do to rectify this?

like image 410
BrynJ Avatar asked Feb 03 '26 17:02

BrynJ


1 Answers

You should use the belongsToMany function with the timestamps declaration like this:

$this->belongsToMany(<entity>)->withTimestamps();
like image 109
Noman Ur Rehman Avatar answered Feb 05 '26 06:02

Noman Ur Rehman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!