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

YasseMoh's avatar

"created at" column not giving correct data

I use laravel 10 and I want to show items in account page. Everything goes well but the item's "created at" column gives a wrong date. this is the controller: public function index() { $data=[]; $user_id=session()->get('user_id'); $data=DB::table('Lasts')->select('Lasts.','categories.')->join('categories','categories.cat_id','=','Lasts.cat_id')->where('Lasts.user_id',$user_id)->orderBy('id','DESC')-> get(); return view('Lasts.index',['Lasts'=>$data,'count'=>$count,'userpic'=>$userpic]); }

this is the model:

namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Last extends Model { use HasFactory; protected $fillable = ['id','title','description','cat_id','user_id','created_at','updated_at']; }

in view: {{$value->created_at}}

0 likes
4 replies
hupp's avatar

i am just reformatting to get better understand first.

Contoller:

public function index()
{
        $data = [];
        $user_id = session()->get('user_id');
        $data = DB::table('Lasts')
            ->select('Lasts.', 'categories.')
            ->join('categories', 'categories.cat_id', '=', 'Lasts.cat_id')
            ->where('Lasts.user_id', $user_id)
            ->orderBy('id', 'DESC')->get();
        
            return view('Lasts.index', ['Lasts' => $data, 'count' => $count, 'userpic' => $userpic]);
}

Model:

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Last extends Model
{
    use HasFactory;
    protected $fillable = ['id', 'title', 'description', 'cat_id', 'user_id', 'created_at', 'updated_at'];
}

View File:

{{$value->created_at}}
Snapey's avatar
Snapey
Best Answer
Level 122

when you use DB you are not using the model at all, so no conversion of dates to Carbon cannot apply

You say the date is wrong, example ?

Be aware that using a join, you could end up with duplicate column names, then it's unpredictable which one you get when you try to get one value

convention states your tables should be lowercase and plural, eg lasts

any reason you dont use eloquent and your models?

1 like
Snapey's avatar

eg using models with the right relationship setup

public function index()
{
        $lasts = Auth::user()->lasts()->with('category')->latest()->get();
        
        return view('Lasts.index', ['Lasts' => $lasts]);
}

Last model

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Last extends Model
{
    use HasFactory;
    protected $fillable = ['id', 'title', 'description', 'cat_id', 'user_id', 'created_at', 'updated_at'];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

PovilasKorop's avatar

@yassemoh what exactly fo you mean by "gives a wrong date"?

What is the date shown and what is the date you expect it to show?

Or do you mean it throws some error?

Please or to participate in this conversation.