2

I try to use $casts to change date format in my Model :

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

It works when I retrieve the data with Eloquent :

$clients = Client::all();
return $clients;

but it doesn't with Query Builder !

$clients = DB::table('clients')->get();
return $clients;
Omar
  • 300
  • 5
  • 14
  • 3
    it works when you retrieve a model data (since $casts is defined in a model) Add an example where it doesnt work for you (btw, builder is still eloquent) – N69S Feb 28 '22 at 23:26
  • I added it, when I use DB::table it doesn't work. – Omar Mar 01 '22 at 07:45
  • 1
    It will never work because there are 2 different classes. Just use Eloquent. You are writing cast to your Model which is Eloquent and trying to run it with DB class. They are not the same. – gguney Mar 01 '22 at 09:22
  • 1
    @gguney @ omar Eloquent is an ORM, when you use DB class, you are using eloquent still. When you use eloquent models, you get instances of the models. bot both are eloquent. – N69S Mar 01 '22 at 09:25
  • @N69S nope you are wrong. Both are not Eloquent. Eloquent is Eloquent but Query Builder is different. Eloquent does have query builder but they are not the same. Here: https://laravel.com/docs/9.x/queries is database query builder and here is eloquent: https://laravel.com/docs/9.x/eloquent – gguney Mar 01 '22 at 09:30
  • @gguney Yes, thanks. – Omar Mar 01 '22 at 09:50

1 Answers1

2

In query builder you are using DB::table it does not use Laravel Model and you are writing this casts code to your Laravel Model. It obviously never work. They are apples and oranges.

If you use casts code, Laravel only runs this code when you get the model and use this field. Before that it does not change the values.

What are you trying to achieve there? If you give us more detailed code I will help you.

gguney
  • 2,512
  • 1
  • 12
  • 26
  • I have a customers table with two fields created_at and updated_at, and I want to fetch records from this table with a date format different from the default one, but I want to use Query Builder instead of Eloquent to minimize the fetch time. – Omar Mar 01 '22 at 07:57
  • 1
    but @Omar that extra fetch time is what casts the data in a different format (+ other things) maybe dont use get() and get all the table in one collection, use pagination – N69S Mar 01 '22 at 08:34
  • @N69S I have a "real time search" feature in the app and that requires to fetch the whole table in one collection. (I use pagination in the front side). – Omar Mar 01 '22 at 09:12
  • You are trying to minimize fetch time in Eloquent with using DB fluent BUT you are using pagination in the frontend??? – gguney Mar 01 '22 at 09:14
  • 2
    @Omar "real time search" doesnt mean get all data. no user can read all 10000000 result at the same time. The pagination need to happen server side not client side to save on performance. – N69S Mar 01 '22 at 09:21
  • Just use Eloquent and where clause to make your search and please forget the "performance" thing in your mind, becuase it almost does not matter DB or Model. – gguney Mar 01 '22 at 09:24
  • @gguney Ok, do you have an example for "real time search" with Eloquent and Where ? regarding performance you can check this https://stackoverflow.com/a/40897636/15012391 – Omar Mar 01 '22 at 09:38
  • @Omar trust me if you are just asking here and you are stuck with basic things, you have to forget about the performance, there are lots of better ways to deal with the performance. Also, this post is from 2016. – gguney Mar 01 '22 at 09:40
  • @N69S please if you have a resource that explains this type of research without get all data, I like to check it. – Omar Mar 01 '22 at 09:43
  • 1
    @omar here is an article about a server side pagination using datatable in the front https://www.nicesnippets.com/blog/laravel-datatables-server-side-example – N69S Mar 01 '22 at 13:06