@@ -32,11 +32,58 @@ public function test_complex_query_are_wrapped_and_countable()
3232 $ this ->assertEquals (60 , $ dataTable ->count ());
3333 }
3434
35+ public function test_complex_query_use_select_in_count ()
36+ {
37+ /** @var \Yajra\DataTables\QueryDataTable $dataTable */
38+ $ dataTable = app ('datatables ' )->of (
39+ DB ::table ('users ' )
40+ ->select ('users.* ' )
41+ ->addSelect ([
42+ 'last_post_id ' => DB ::table ('posts ' )
43+ ->whereColumn ('posts.user_id ' , 'users.id ' )
44+ ->orderBy ('created_at ' )
45+ ->select ('id ' )
46+ ])
47+ ->orderBy (DB ::table ('posts ' )->whereColumn ('posts.user_id ' , 'users.id ' )->orderBy ('created_at ' )->select ('created_at ' )
48+ )
49+ );
50+
51+ $ this ->assertQueryHasNoSelect (false , $ dataTable ->prepareCountQuery ());
52+ $ this ->assertEquals (20 , $ dataTable ->count ());
53+ }
54+
55+ public function test_complex_query_can_ignore_select_in_count ()
56+ {
57+ /** @var \Yajra\DataTables\QueryDataTable $dataTable */
58+ $ dataTable = app ('datatables ' )->of (
59+ DB ::table ('users ' )
60+ ->select ('users.* ' )
61+ ->addSelect ([
62+ 'last_post_id ' => DB ::table ('posts ' )
63+ ->whereColumn ('posts.user_id ' , 'users.id ' )
64+ ->orderBy ('created_at ' )
65+ ->select ('id ' )
66+ ])
67+ ->orderBy (DB ::table ('posts ' )->whereColumn ('posts.user_id ' , 'users.id ' )->orderBy ('created_at ' )->select ('created_at ' )
68+ )
69+ )->ignoreSelectsInCountQuery ();
70+
71+ $ this ->assertQueryHasNoSelect (true , $ dataTable ->prepareCountQuery ());
72+ $ this ->assertEquals (20 , $ dataTable ->count ());
73+ }
74+
3575 public function test_simple_queries_with_complexe_select_are_wrapped_without_selects ()
3676 {
3777 /** @var \Yajra\DataTables\QueryDataTable $dataTable */
3878 $ dataTable = app ('datatables ' )->of (
39- DB ::table ('users ' )->select ('users.* ' )->selectRaw ('(select id from posts where posts.user_id = users.id order by created_at desc limit 1) as last_post_id ' )
79+ DB ::table ('users ' )
80+ ->select ('users.* ' )
81+ ->addSelect ([
82+ 'last_post_id ' => DB ::table ('posts ' )
83+ ->whereColumn ('posts.user_id ' , 'users.id ' )
84+ ->orderBy ('created_at ' )
85+ ->select ('id ' )
86+ ])
4087 );
4188
4289 $ this ->assertQueryWrapped (true , $ dataTable ->prepareCountQuery ());
@@ -55,6 +102,17 @@ public function test_simple_queries_are_not_wrapped_and_countable()
55102 $ this ->assertEquals (20 , $ dataTable ->count ());
56103 }
57104
105+ public function test_complexe_queries_can_be_wrapped_and_countable ()
106+ {
107+ /** @var \Yajra\DataTables\QueryDataTable $dataTable */
108+ $ dataTable = app ('datatables ' )->of (
109+ User::with ('posts ' )->select ('users.* ' )
110+ );
111+
112+ $ this ->assertQueryWrapped (false , $ dataTable ->prepareCountQuery ());
113+ $ this ->assertEquals (20 , $ dataTable ->count ());
114+ }
115+
58116 /**
59117 * @param $expected bool
60118 * @param $query \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
0 commit comments