laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.51k stars 11.02k forks source link

Timestamps error when ->using('PivotModel') #21766

Closed projct1 closed 7 years ago

projct1 commented 7 years ago

Description:

class Order extends Model
{
    public function sms()
    {
        return $this->belongsToMany('App\Sms')->using('App\OrderSms')->withTimestamps();
    }
}

class Sms extends Model
{
    public $timestamps = false;

    protected $fillable = ['subject', 'template'];
}

class OrderSms extends Pivot
{
    protected $table = 'order_sms';
}

OrderSms::where('red_sms_id', $smsId)->update([
    'status' => $status
]);

As result is Call to a member function getUpdatedAtColumn() on null. I have to use:

DB::table('order_sms')->where('red_sms_id', $smsId)->update([
    'status' => $status,
    'updated_at' => Carbon::now()
]);
themsaid commented 7 years ago

Please ask on the forums, this repo is for bug reporting only. You can use https://laracasts.com/discuss or https://laravel.io/forum which are forums with a very large community of developers helping each other.

projct1 commented 7 years ago

But ist bug! Expected behaviour is updating timestamps on OrderSms table.

projct1 commented 7 years ago

@themsaid I updated code.

zecipriano commented 6 years ago

I also think this is a bug. @themsaid, please review.

The issue is that when you load a Pivot Model directly (like in the example above) the $pivotParent property is not set.

Then, the getCreatedAtColumn() and getUpdatedAtColumn() functions fail because they reference that $pivotParent property.

A possible solution is to return the model own timestamps when no $pivotParent is set.