File tree Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Expand file tree Collapse file tree 2 files changed +30
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Yajra \DataTables \Transformers ;
4
4
5
+ use Closure ;
5
6
use Illuminate \Support \Collection as LaravelCollection ;
6
7
use League \Fractal \Manager ;
7
8
use League \Fractal \Resource \Collection ;
@@ -82,11 +83,11 @@ protected function createSerializer(SerializerAbstract|string $serializer): Seri
82
83
* Get or create transformer instance.
83
84
*
84
85
* @param \Closure|class-string|TransformerAbstract $transformer
85
- * @return \League\Fractal\TransformerAbstract
86
+ * @return \Closure|\ League\Fractal\TransformerAbstract
86
87
*/
87
- protected function createTransformer (\ Closure |string |TransformerAbstract $ transformer ): TransformerAbstract
88
+ protected function createTransformer (Closure |string |TransformerAbstract $ transformer ): Closure | TransformerAbstract
88
89
{
89
- if ($ transformer instanceof TransformerAbstract || $ transformer instanceof \ Closure) {
90
+ if ($ transformer instanceof TransformerAbstract || $ transformer instanceof Closure) {
90
91
return $ transformer ;
91
92
}
92
93
Original file line number Diff line number Diff line change @@ -25,6 +25,21 @@ public function it_can_transform_response()
25
25
$ this ->assertIsString ($ json ['data ' ][0 ]['name ' ]);
26
26
}
27
27
28
+ /** @test */
29
+ public function it_works_with_closure ()
30
+ {
31
+ $ json = $ this ->getAjax ('/closure ' );
32
+
33
+ $ json ->assertJson ([
34
+ 'draw ' => 0 ,
35
+ 'recordsTotal ' => 20 ,
36
+ 'recordsFiltered ' => 20 ,
37
+ ]);
38
+
39
+ $ this ->assertIsInt ($ json ['data ' ][0 ]['id ' ]);
40
+ $ this ->assertIsString ($ json ['data ' ][0 ]['name ' ]);
41
+ }
42
+
28
43
protected function setUp (): void
29
44
{
30
45
parent ::setUp ();
@@ -34,5 +49,16 @@ protected function setUp(): void
34
49
->setTransformer (UserTransformer::class)
35
50
->toJson ();
36
51
});
52
+
53
+ $ this ->app ['router ' ]->get ('/closure ' , function () {
54
+ return datatables ()->eloquent (User::query ())
55
+ ->setTransformer (function (User $ user ) {
56
+ return [
57
+ 'id ' => (int ) $ user ->id ,
58
+ 'name ' => $ user ->name ,
59
+ ];
60
+ })
61
+ ->toJson ();
62
+ });
37
63
}
38
64
}
You can’t perform that action at this time.
0 commit comments