@@ -23,95 +23,119 @@ public function search(Request $request): JsonResponse
2323
2424 // Invoices
2525 Invoice::where ('tenant_id ' , $ tenantId )
26- ->where (fn ($ query ) => $ query ->where ('number ' , 'like ' , "% {$ q }% " )
27- ->orWhereHas ('contact ' , fn ($ q2 ) => $ q2 ->where ('name ' , 'like ' , "% {$ q }% " )))
26+ ->where (function ($ query ) use ($ q ) {
27+ $ query ->where ('number ' , 'like ' , "% {$ q }% " )
28+ ->orWhereHas ('contact ' , fn ($ q2 ) => $ q2 ->where ('name ' , 'like ' , "% {$ q }% " ));
29+ })
2830 ->limit (5 )->get ()
29- ->each (fn ($ inv ) => $ results [] = [
30- 'module ' => 'invoice ' ,
31- 'id ' => $ inv ->id ,
32- 'title ' => "Invoice # {$ inv ->number }" ,
33- 'subtitle ' => $ inv ->status ,
34- 'url ' => "/finance/invoices/ {$ inv ->id }" ,
35- ]);
31+ ->each (function ($ inv ) use (&$ results ) {
32+ $ results [] = [
33+ 'module ' => 'invoice ' ,
34+ 'id ' => $ inv ->id ,
35+ 'title ' => "Invoice # {$ inv ->number }" ,
36+ 'subtitle ' => $ inv ->status ,
37+ 'url ' => "/finance/invoices/ {$ inv ->id }" ,
38+ ];
39+ });
3640
3741 // Contacts
3842 Contact::where ('tenant_id ' , $ tenantId )
39- ->where (fn ($ query ) => $ query ->where ('name ' , 'like ' , "% {$ q }% " )
40- ->orWhere ('email ' , 'like ' , "% {$ q }% " ))
43+ ->where (function ($ query ) use ($ q ) {
44+ $ query ->where ('name ' , 'like ' , "% {$ q }% " )
45+ ->orWhere ('email ' , 'like ' , "% {$ q }% " );
46+ })
4147 ->limit (5 )->get ()
42- ->each (fn ($ c ) => $ results [] = [
43- 'module ' => 'contact ' ,
44- 'id ' => $ c ->id ,
45- 'title ' => $ c ->name ,
46- 'subtitle ' => $ c ->type ,
47- 'url ' => "/finance/contacts/ {$ c ->id }" ,
48- ]);
48+ ->each (function ($ c ) use (&$ results ) {
49+ $ results [] = [
50+ 'module ' => 'contact ' ,
51+ 'id ' => $ c ->id ,
52+ 'title ' => $ c ->name ,
53+ 'subtitle ' => $ c ->type ,
54+ 'url ' => "/finance/contacts/ {$ c ->id }" ,
55+ ];
56+ });
4957
5058 // Products
5159 Product::where ('tenant_id ' , $ tenantId )
52- ->where (fn ($ query ) => $ query ->where ('name ' , 'like ' , "% {$ q }% " )
53- ->orWhere ('sku ' , 'like ' , "% {$ q }% " ))
60+ ->where (function ($ query ) use ($ q ) {
61+ $ query ->where ('name ' , 'like ' , "% {$ q }% " )
62+ ->orWhere ('sku ' , 'like ' , "% {$ q }% " );
63+ })
5464 ->limit (5 )->get ()
55- ->each (fn ($ p ) => $ results [] = [
56- 'module ' => 'product ' ,
57- 'id ' => $ p ->id ,
58- 'title ' => $ p ->name ,
59- 'subtitle ' => $ p ->sku ,
60- 'url ' => "/inventory/products/ {$ p ->id }" ,
61- ]);
65+ ->each (function ($ p ) use (&$ results ) {
66+ $ results [] = [
67+ 'module ' => 'product ' ,
68+ 'id ' => $ p ->id ,
69+ 'title ' => $ p ->name ,
70+ 'subtitle ' => $ p ->sku ,
71+ 'url ' => "/inventory/products/ {$ p ->id }" ,
72+ ];
73+ });
6274
6375 // Employees
6476 Employee::where ('tenant_id ' , $ tenantId )
65- ->where (fn ($ query ) => $ query ->where ('first_name ' , 'like ' , "% {$ q }% " )
66- ->orWhere ('last_name ' , 'like ' , "% {$ q }% " )
67- ->orWhere ('email ' , 'like ' , "% {$ q }% " ))
77+ ->where (function ($ query ) use ($ q ) {
78+ $ query ->where ('first_name ' , 'like ' , "% {$ q }% " )
79+ ->orWhere ('last_name ' , 'like ' , "% {$ q }% " )
80+ ->orWhere ('email ' , 'like ' , "% {$ q }% " );
81+ })
6882 ->limit (5 )->get ()
69- ->each (fn ($ e ) => $ results [] = [
70- 'module ' => 'employee ' ,
71- 'id ' => $ e ->id ,
72- 'title ' => "{$ e ->first_name } {$ e ->last_name }" ,
73- 'subtitle ' => $ e ->email ,
74- 'url ' => "/hr/employees/ {$ e ->id }" ,
75- ]);
83+ ->each (function ($ e ) use (&$ results ) {
84+ $ results [] = [
85+ 'module ' => 'employee ' ,
86+ 'id ' => $ e ->id ,
87+ 'title ' => "{$ e ->first_name } {$ e ->last_name }" ,
88+ 'subtitle ' => $ e ->email ,
89+ 'url ' => "/hr/employees/ {$ e ->id }" ,
90+ ];
91+ });
7692
7793 // CRM Leads
7894 CrmLead::where ('tenant_id ' , $ tenantId )
79- ->where (fn ($ query ) => $ query ->where ('contact_name ' , 'like ' , "% {$ q }% " )
80- ->orWhere ('company_name ' , 'like ' , "% {$ q }% " )
81- ->orWhere ('email ' , 'like ' , "% {$ q }% " )
82- ->orWhere ('reference ' , 'like ' , "% {$ q }% " ))
95+ ->where (function ($ query ) use ($ q ) {
96+ $ query ->where ('contact_name ' , 'like ' , "% {$ q }% " )
97+ ->orWhere ('company_name ' , 'like ' , "% {$ q }% " )
98+ ->orWhere ('email ' , 'like ' , "% {$ q }% " )
99+ ->orWhere ('reference ' , 'like ' , "% {$ q }% " );
100+ })
83101 ->limit (5 )->get ()
84- ->each (fn ($ l ) => $ results [] = [
85- 'module ' => 'lead ' ,
86- 'id ' => $ l ->id ,
87- 'title ' => $ l ->contact_name ?? $ l ->company_name ?? $ l ->reference ,
88- 'subtitle ' => $ l ->status ?? '' ,
89- 'url ' => "/crm/leads/ {$ l ->id }" ,
90- ]);
102+ ->each (function ($ l ) use (&$ results ) {
103+ $ results [] = [
104+ 'module ' => 'lead ' ,
105+ 'id ' => $ l ->id ,
106+ 'title ' => $ l ->contact_name ?? $ l ->company_name ?? $ l ->reference ,
107+ 'subtitle ' => $ l ->status ?? '' ,
108+ 'url ' => "/crm/leads/ {$ l ->id }" ,
109+ ];
110+ });
91111
92112 // Projects
93113 Project::where ('tenant_id ' , $ tenantId )
94114 ->where ('name ' , 'like ' , "% {$ q }% " )
95115 ->limit (5 )->get ()
96- ->each (fn ($ p ) => $ results [] = [
97- 'module ' => 'project ' ,
98- 'id ' => $ p ->id ,
99- 'title ' => $ p ->name ,
100- 'subtitle ' => $ p ->status ,
101- 'url ' => "/pm/projects/ {$ p ->id }" ,
102- ]);
116+ ->each (function ($ p ) use (&$ results ) {
117+ $ results [] = [
118+ 'module ' => 'project ' ,
119+ 'id ' => $ p ->id ,
120+ 'title ' => $ p ->name ,
121+ 'subtitle ' => $ p ->status ,
122+ 'url ' => "/pm/projects/ {$ p ->id }" ,
123+ ];
124+ });
103125
104126 // Purchase Orders
105127 PurchaseOrder::where ('tenant_id ' , $ tenantId )
106- ->where (fn ( $ query ) => $ query -> where ( 'po_number ' , 'like ' , "% {$ q }% " ) )
128+ ->where ('po_number ' , 'like ' , "% {$ q }% " )
107129 ->limit (5 )->get ()
108- ->each (fn ($ po ) => $ results [] = [
109- 'module ' => 'purchase_order ' ,
110- 'id ' => $ po ->id ,
111- 'title ' => "PO # {$ po ->po_number }" ,
112- 'subtitle ' => $ po ->status ,
113- 'url ' => "/purchase/orders/ {$ po ->id }" ,
114- ]);
130+ ->each (function ($ po ) use (&$ results ) {
131+ $ results [] = [
132+ 'module ' => 'purchase_order ' ,
133+ 'id ' => $ po ->id ,
134+ 'title ' => "PO # {$ po ->po_number }" ,
135+ 'subtitle ' => $ po ->status ,
136+ 'url ' => "/purchase/orders/ {$ po ->id }" ,
137+ ];
138+ });
115139
116140 return $ this ->success (['query ' => $ q , 'results ' => $ results , 'total ' => count ($ results )]);
117141 }
0 commit comments