|
我想进行一个高级搜索,其中用户要搜索可选参数,将来自三个表的数据连接如下9 [- g" ]; {. ~0 H
$qry="SELECT rooms.*, salereservation.*, customers.*
6 M- \+ Q& ?& d z! H4 m FROM rooms
4 G1 Z4 c, O3 U4 a7 Z+ a JOIN salereservation
3 ~9 W* ]- _; G5 s ON salereservation.room_id = rooms.room_id
) B* A% t1 |9 m3 ?" z4 ] JOIN customers. U3 e; u4 M/ ~
ON customers.id = salereservation.customer_id* { s% j [4 G
WHERE salereservation.sale_status=1 AND ";
3 H0 T' R' B' |" K7 u我将附加到查询字符串的可选参数如下
8 `7 L' G. E* o- S2 Fif($fname!=''){
+ C# L) X3 A1 `+ t5 M' r $qry.="fname LIKE %fname% AND ";0 K- ]- k6 U' P' O9 Q D
}
) Y) S1 h U6 Y5 w if($lname!=''){6 w7 S" n9 s# v2 d+ O H8 w# \ |
$qry.="lname LIKE %:lname% AND ";
8 j4 @5 f9 }3 e% q. a) K }
$ f0 V0 [6 r' X( ? if($time_in!=''){- s/ S! x4 {- [6 f6 f
$qry.="start_datetime LIKE %time_in% AND ";* B* y* p7 ?% }* Y
}/ I/ r0 u1 E/ s& c- h' b) Y2 b* w6 Z
if($time_out!=''){/ U6 [ Y+ k* N& ]
$qry.="end_datetime LIKE %time_out% AND ";& y7 O: e$ W! a% N0 T* \8 x$ y
}
* w4 ]* m, M6 Q L2 b2 \) d, S7 x- Y# ` if($phone!=''){
3 u8 v1 S, N% Q7 M $qry.="phone LIKE %phone% AND ";
+ M& h$ p" N" [3 C3 t3 V }& [! N r5 O$ O0 y% |
if($room_no!=''){
2 h" U- a0 i0 N $qry.="room_no LIKE %room_no%";
) [! L6 w3 e" U, {; P5 d }( j1 N. i0 `5 Y/ r4 K6 @9 C
我的问题是如何将代码转换为laravel查询生成器0 m# w. z! E& ~
) ?5 d8 q9 {' n8 q' u& O/ c0 B解决方案:
3 B3 d. g Y! b' J1 T- D
/ m- z4 j( a* m: u& y' q5 a2 |) ?( t4 ]. {
0 s5 a8 F" \ B" c! ~/ X
我没有尝试过此代码,但它应在laravel 4.2下工作。9 f" c$ ~3 @ ], O
$query = DB::table('rooms')- h, v" e) u; }& p: x
->join("salereservation", "salereservation.room_id", "=", "rooms.room_id")
0 @' [$ v5 C# R7 D7 M# K( ^1 ?! t ->join("customers", "customers.id", "=", "salereservation.customer_id")% I% B& |& p) d& O* Q0 L
->where("salereservation.sale_status",'=',1)
2 K1 H- ~" P+ ~2 d& i ->select('rooms.*', 'salereservation.*', 'customers.*');
# s8 [& m9 f: u4 c3 Kif($fname!=''){/ S) \& @6 X7 F
$query->where("fname",'like',"%$fname%");
, r3 r/ b. X4 j2 G. x+ \4 ]( J}
* z) L# h, V7 @" A$ n" tif($lname!=''){
, c. N# r+ D9 [ |7 N2 c $query->where("lname",'like',"%$lname%");
5 `" U+ V# O* M4 Y' M# j}
" L2 j5 y( z3 t% K" ]/ l4 zif($time_in!=''){
5 F: O4 _- |& B$ ` $query->where("start_datetime",'like',"%$time_in%");
0 y& y: w) ^! |" u+ H: m! B( S4 A; Z}
- B, K* S' F5 A( H2 }if($time_out!=''){
' M' S. T5 t. X( O) q" h. T $query->where("end_datetime",'like',"%$time_out%");! T" a' `3 U0 f* n
}4 p" m) h! g; b4 c
if($phone!=''){
9 ]4 C* _: N) t4 e s $query->where("phone",'like',"%$phone%");$ D' R2 U& y# p3 X! \3 g
}# F# Q$ O# ]# M2 ^
if($room_no!=''){
8 [1 t3 D' a# I+ q $query->where("room_no",'like',"%$room_no%");
. R2 z2 e% X5 M. y- ], m}! L% \+ D% E# a4 c8 L. ?
$data = $query->get(); //finally get the result
/ J$ L6 b0 t8 J7 L更新:
* ~; b5 O0 ~# H* s) ~为了进行查询验证,您可以使用以下命令打印查询:
( C& @9 m& `# D% W% }4 C. _7 u $queries = DB::getQueryLog();5 M v5 k$ x7 Z6 x H# z7 X$ y6 t/ C
$last_query = end($queries);
, a" r3 t) r$ ?3 u& N9 } dd($last_query);; H) C3 e3 H& c3 w% `6 Q0 C7 g
并验证您的查询是否与所需查询不同。如果事情有所不同,我们可以根据它们升级我们的查询结构。您还可以使用从laravel方法生成的最新查询进行更新。! p: V, S- g( M& n8 f
但是如果您仍然难以理解我的观点。让我知道。 |
|