File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace Yajra \DataTables \Utilities ;
4
4
5
+ use Closure ;
5
6
use DateTime ;
6
7
use Illuminate \Contracts \Support \Arrayable ;
7
8
use Illuminate \Support \Arr ;
8
9
use Illuminate \Support \Str ;
10
+ use ReflectionFunction ;
9
11
10
12
class Helper
11
13
{
@@ -58,12 +60,23 @@ protected static function isItemOrderInvalid($item, $array)
58
60
* @param array $data data to use with blade template
59
61
* @param array|object $param parameter to call with callable
60
62
* @return mixed
63
+ *
64
+ * @throws \ReflectionException
61
65
*/
62
66
public static function compileContent ($ content , array $ data , array |object $ param )
63
67
{
64
68
if (is_string ($ content )) {
65
69
return static ::compileBlade ($ content , static ::getMixedValue ($ data , $ param ));
66
- } elseif (is_callable ($ content )) {
70
+ }
71
+
72
+ if ($ content instanceof Closure) {
73
+ $ reflection = new ReflectionFunction ($ content );
74
+ $ arguments = $ reflection ->getParameters ();
75
+
76
+ if (count ($ arguments ) > 0 ) {
77
+ return app ()->call ($ content , [$ arguments [0 ]->name => $ param ]);
78
+ }
79
+
67
80
return $ content ($ param );
68
81
}
69
82
Original file line number Diff line number Diff line change @@ -339,6 +339,26 @@ public function it_can_return_formatted_columns()
339
339
$ this ->assertEquals (Carbon::parse ($ user ->created_at )->format ('Y-m-d ' ), $ data ['created_at_formatted ' ]);
340
340
}
341
341
342
+ /** @test */
343
+ public function it_can_return_added_column_with_dependency_injection ()
344
+ {
345
+ $ crawler = $ this ->call ('GET ' , '/closure-di ' );
346
+
347
+ $ crawler ->assertJson ([
348
+ 'draw ' => 0 ,
349
+ 'recordsTotal ' => 20 ,
350
+ 'recordsFiltered ' => 20 ,
351
+ ]);
352
+
353
+ $ user = DB ::table ('users ' )->find (1 );
354
+ $ data = $ crawler ->json ('data ' )[0 ];
355
+
356
+ $ this ->assertTrue (isset ($ data ['name ' ]));
357
+ $ this ->assertTrue (isset ($ data ['name_di ' ]));
358
+
359
+ $ this ->assertEquals ($ user ->name .'_di ' , $ data ['name_di ' ]);
360
+ }
361
+
342
362
protected function setUp (): void
343
363
{
344
364
parent ::setUp ();
@@ -451,5 +471,13 @@ protected function setUp(): void
451
471
->setFilteredRecords (10 )
452
472
->toJson ();
453
473
});
474
+
475
+ $ router ->get ('/closure-di ' , function (DataTables $ dataTable ) {
476
+ return $ dataTable ->query (DB ::table ('users ' ))
477
+ ->addColumn ('name_di ' , function ($ user , User $ u ) {
478
+ return $ u ->newQuery ()->find ($ user ->id )->name .'_di ' ;
479
+ })
480
+ ->toJson ();
481
+ });
454
482
}
455
483
}
You can’t perform that action at this time.
0 commit comments