@@ -121,6 +121,46 @@ public function it_can_return_formatted_columns()
121121 $ this ->assertEquals (Carbon::parse ($ user ->created_at )->format ('Y-m-d ' ), $ data ['created_at_formatted ' ]);
122122 }
123123
124+ /** @test */
125+ public function it_can_return_formatted_column_using_closure ()
126+ {
127+ $ crawler = $ this ->call ('GET ' , '/eloquent/formatColumn-closure ' );
128+
129+ $ crawler ->assertJson ([
130+ 'draw ' => 0 ,
131+ 'recordsTotal ' => 20 ,
132+ 'recordsFiltered ' => 20 ,
133+ ]);
134+
135+ $ user = User::find (1 );
136+ $ data = $ crawler ->json ('data ' )[0 ];
137+
138+ $ this ->assertTrue (isset ($ data ['created_at ' ]));
139+ $ this ->assertTrue (isset ($ data ['created_at_formatted ' ]));
140+
141+ $ this ->assertEquals (Carbon::parse ($ user ->created_at )->format ('Y-m-d ' ), $ data ['created_at_formatted ' ]);
142+ }
143+
144+ /** @test */
145+ public function it_can_return_formatted_column_on_invalid_formatter ()
146+ {
147+ $ crawler = $ this ->call ('GET ' , '/eloquent/formatColumn-fallback ' );
148+
149+ $ crawler ->assertJson ([
150+ 'draw ' => 0 ,
151+ 'recordsTotal ' => 20 ,
152+ 'recordsFiltered ' => 20 ,
153+ ]);
154+
155+ $ user = User::find (1 );
156+ $ data = $ crawler ->json ('data ' )[0 ];
157+
158+ $ this ->assertTrue (isset ($ data ['created_at ' ]));
159+ $ this ->assertTrue (isset ($ data ['created_at_formatted ' ]));
160+
161+ $ this ->assertEquals ($ user ->created_at , $ data ['created_at_formatted ' ]);
162+ }
163+
124164 /** @test */
125165 public function it_accepts_a_relation ()
126166 {
@@ -152,5 +192,17 @@ protected function setUp(): void
152192 ->formatColumn ('created_at ' , new DateFormatter ('Y-m-d ' ))
153193 ->toJson ();
154194 });
195+
196+ $ router ->get ('/eloquent/formatColumn-closure ' , function (DataTables $ dataTable ) {
197+ return $ dataTable ->eloquent (User::query ())
198+ ->formatColumn ('created_at ' , fn ($ value , $ row ) => Carbon::parse ($ value )->format ('Y-m-d ' ))
199+ ->toJson ();
200+ });
201+
202+ $ router ->get ('/eloquent/formatColumn-fallback ' , function (DataTables $ dataTable ) {
203+ return $ dataTable ->eloquent (User::query ())
204+ ->formatColumn ('created_at ' , 'InvalidFormatter::class ' )
205+ ->toJson ();
206+ });
155207 }
156208}
0 commit comments