Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

melx's avatar
Level 4

calculated : Date From To in blade

I want to show the date from and To by calculate from create date

example: created_at = 2020-11-19 12:54:18 so i want to calculate the 7 days from this date and show that end date of 7 days and display it as To date in the blade.

0 likes
12 replies
automica's avatar

@emfinanga what do you mean

so i want to calculate the save date from this date

what is 'save' date?

if you just want to display created_at + 7 days

$model->created_at->addDays(7);
Snapey's avatar
{{ $model->created_at->addDays(7) }}

but I would not put a 'magic number' like 7 in the view

melx's avatar
Level 4

i get this error in blade

          Call to a member function addDays() on string
automica's avatar

@emfinanga in you model cast the field to date

 protected $casts = [
        'created_at'=> 'date',
    ];
MichalOravec's avatar

You can use accessor

public function getToDateAttribute()
{
    return $this->created_at->addDays(7);
}
{{ $model->toDate }}
1 like
melx's avatar
Level 4

this is my controller

        public function print_sales_bill($bill_number){
        // $billsale=Sales::find($bill_number);

            $billsale=DB::table('sales')

                   ->join('customers','customers.id','=','sales.client_id')
                   ->join('users','users.id','=','sales.tag_id')
                   ->join('customer__orders','customer__orders.id','=','sales.TruckNo_id')
                   ->join('issue_to_sales','issue_to_sales.id','=','sales.unit_issue_id')
                   ->join('locations','locations.id','=','customer__orders.border_id')
                   ->join('devices','devices.id','=','issue_to_sales.unit_id')
                   ->Join("devices as devices2",\DB::raw("FIND_IN_SET(devices2.id,issue_to_sales.unit_tag_id)"),">",\DB::raw("'0'"))

                   ->where('bill_number',$bill_number)
                   ->first(['customers.*','users.*','customer__orders.*','issue_to_sales.*','locations.*','sales.*','devices2.*','devices.*',\DB::raw("group_concat(devices2.Devicenumber SEPARATOR ', ') as slavename"),'sales.created_at as created_at_sale']);


                return view('sales.printreceipt',compact('billsale'));

                   // return response()->json($billsale);
        }

as you can see created_at_sale

in blade

             <P>The Rental of This Device(s) is Valid in 7 days: <b>From {{$billsale->created_at_sale}} TO {{ $billsale->created_at_sale->addWeek() }}</b></P>
MichalOravec's avatar
Level 75

So you don't work with models, you need to parse it to carbon instance

{{ \Carbon\Carbon::parse($billsale->created_at_sale)->addWeek() }}

Please or to participate in this conversation.