0

I have my Model Sales Target when I create and update Sales Target, Sales Target has the start_date and end_date of the sales target the problem is that my written function which I wrote Inside my SaleTarget model then the start_date filed is also updating with created_at timestamp which I don't want so I don't know to prevent it by updating my start_date. My Model Code is below

class SalesTarget extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'sales_target_amount',
        'achieved_sales_target_amount',
        'commission_on_target',
        'is_achieved',
        'is_paid',
        'remarks',
    ];

    protected $dates = ['start_date', 'end_date'];


    public function agent()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    // Get the Achieved Sales Target of Particular Target with Sales Agent
    public function achieved_target($id, $sales_agent, $start_date, $end_date)
    {
        // For Submission of Package Commission
        $package_commission = Commission::whereDate('created_at','>=',$start_date->format("Y-m-d"))->whereDate('created_at','<=',$end_date->format('Y-m-d'))->where('user_id', $sales_agent)->where('status', 0)->sum('package_price');
        // // For Submission of Domain Commission
        $domain_commission = DomainCommission::whereDate('created_at', '>=',  $start_date->format('Y-m-d'))->where('created_at', '<=', $end_date->format('Y-m-d'))->where('user_id', $sales_agent)->where('status', 0)->sum('domain_price');

        // Updating Achieved Sales Target
        $target = SalesTarget::findOrFail($id);
        $target->achieved_sales_target_amount = ($package_commission+$domain_commission);


        if($package_commission+$domain_commission>=$target->sales_target_amount)
        {
            $target->is_achieved = 1;
        }
        else
        {
            $target->is_achieved = 0;
        }
        $target->update();
        // return ($package_commission+$domain_commission);
        return ($target->achieved_sales_target_amount);
    }

when *** $target->update() *** execute then my start_date is modifying with created_at timestamps. Any Suggestions to prevent updating start_date timestamps form created_at timestamps?

Adnan Ali
  • 61
  • 6

1 Answers1

1

Try changing

protected $dates = ['start_date', 'end_date'];

to

protected $casts = [
  'start_date' => 'date',
  'end_date' => 'date'
];
  • That was my gut reaction too, as protected $dates was deprecated in Laravel 8. It still works, though - I've just tried the above on a Laravel 9 installation, and cannot replicate the issue. It is possible to define different columns to be used as the created_at and updated_at columns, but as that would be defined in the model itself, that doesn't appear to be the case here. – Giles Bennett Oct 27 '22 at 09:14
  • but with $casts we cant use functions like format('d/m/Y'); – Adnan Ali Oct 28 '22 at 05:31
  • protected $casts also not working I can't understand why just start_date is appending with current timestamps on the other side end_date is not effecting. – Adnan Ali Oct 28 '22 at 06:23
  • Does it do the same if you remove both $casts and $dates? Just to understand if the problem comes from those lines. – Samuele Cavalleri Oct 28 '22 at 08:44
  • Yeah it does the same and updates the start_date with the current timestamp – Adnan Ali Oct 28 '22 at 09:19
  • I also used $target->update(['is_achieved' => $is_achieved], ['timestamps' => false ]); to disable timestamps but it does same with this too – Adnan Ali Oct 28 '22 at 09:20