Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] withTimestamps() never applied to custom pivot model #23601

Closed
stayallive opened this issue Mar 18, 2018 · 2 comments
Closed

[5.6] withTimestamps() never applied to custom pivot model #23601

stayallive opened this issue Mar 18, 2018 · 2 comments

Comments

@stayallive
Copy link
Contributor

stayallive commented Mar 18, 2018

  • Laravel Version: 5.6.12
  • PHP Version: 7.2.0
  • Database Driver & Version: MySQL 5.7.20

Description:

When calling ->withTimestamps() on a belongs to many relation it is never applied to a custom pivot model registered with ->using().

Steps To Reproduce:

Create a model for example Domains that belongs to many to Certificates using the following relation method:

use Illuminate\Database\Eloquent\Model;

class Domain extends Model 
{
    public function certificates()
    {
        return $this->belongsToMany(Certificate::class)
                    ->using(CertificateDomain::class)
                    ->withTimestamps();
    }
}

The pivot model was reduced to the bare minimum to rule out a user error:

use Illuminate\Database\Eloquent\Relations\Pivot;

class CertificateDomain extends Pivot
{
}

Observe that when updating the pivot model via the updated_at will not be updated:

$certificate = Domain::find(1)->certificates()->first();
$certificate->pivot->some_column = 'something';
$certificate->pivot->save();

When confirming the timestamps attribute on the model is true we see it's not:

$certificate = Domain::find(1)->certificates()->first();
dd($certificate->pivot->timestamps); // false

When defining the relationship like this:

use Illuminate\Database\Eloquent\Model;

class Domain extends Model 
{
    public function certificates()
    {
        return $this->belongsToMany(Certificate::class)
                    //->using(CertificateDomain::class)
                    ->withTimestamps();
    }
}

The updated_at column will be updated when the pivot model is saved.

@stayallive stayallive changed the title withTimestamps() never applied to custom pivot model [5.6] withTimestamps() never applied to custom pivot model Mar 18, 2018
@cetetesoft
Copy link

I've had the same issue. Apparently the custom pivot is created using Pivot::fromAttributes() with an empty array as $attributes, so $instance->timestamps is always set to false.

@luchaos
Copy link
Contributor

luchaos commented Apr 19, 2018

It took me ages to find out but came to the same conclusion (before finding this issue):
a738129#r28662738
Thanks for showing me the ->using(CertificateDomain::class) line btw @stayallive - i can finally remove all those verbose newPivot() overrides within my custom pivot classes. Just found out that's in the docs since 5.4; the more you know...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants