'Using Carbon diffForHumans in my model in laravel 8

I am using laravel 8 framework as my backend for my android application and I want to output time the same way instagram shows time but each time I use carbon diffForHumans it shows 2hours from now and I want it to show 1 minute ago, just now, 2 weeks ago,2 hours ago. here is my model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Carbon\Carbon;

class News extends Model
{
    use HasFactory;

    protected $table = "news";

    protected $fillable = [
        'title',
        'image',
        'body'
    ];

    protected $casts = [
      'created_at' => 'datetime:d-m-Y',
      'updated_at' => 'datetime:d-m-Y'
    ];

    protected $appends=['published'];

    public function getPublishedAttribute(){
        
        return Carbon::createFromTimeStamp(strtotime($this->attributes['created_at']) )->diffForHumans();

    }

    
}

here is my controller

<?php

namespace App\Http\Controllers;

use App\News;
use Illuminate\Http\Request;

class NewsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $allposts = News:://get()->sortByDesc(function($query){
           // return $query->toArray(['id']);
        //})->
        all();

        return response()->json(['newsitemlist' => $allposts], 200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $allposts = News::find($id);
        if (is_null($allposts)) {
            return response()->json(['message' => "News not available"], 404);
        }
        return response()->json(['newsitemlist' => $allposts], 200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $allposts = News::find($id);
        if (is_null($allposts)) {
            return response()->json(['message' => "News not available"], 404);
        }

        $allposts->delete();

        return response()->json(['message' => "News Deleted Succesfully"], 200);
    }
}




Solution 1:[1]

You can also use

public function getCreatedAtAttribute($value){
    return Carbon::parse($value)->diffForHumans();
}

Solution 2:[2]

Based on Docs, this is what you need to do to modify created_at value. Its called Accessor

public function getCreatedAtAttribute($value){
    return Carbon::createFromFormat('Y-m-d H:i:s', $value)->diffForHumans();
}

Also you can check more about Carbon API here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bojan S.
Solution 2 Localhousee