Reputation: 61
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?
Upvotes: 0
Views: 398
Reputation: 61
Try changing
protected $dates = ['start_date', 'end_date'];
to
protected $casts = [
'start_date' => 'date',
'end_date' => 'date'
];
Upvotes: 1