@@ -327,6 +327,93 @@ public function orWhere($condition, $comparator = null, $value = null): self
327327 return $ this ;
328328 }
329329
330+ /**
331+ * Add a JSON where clause to the query
332+ *
333+ * @param string $column The JSON column
334+ * @param string $jsonKey The key within the JSON structure
335+ * @param mixed $value The value to compare against
336+ * @param string $comparator The comparison operator (default '=')
337+ */
338+ public function whereJson (string $ column , string $ jsonKey , $ value , string $ comparator = '= ' ): self
339+ {
340+ // Check if the value is an integer, and cast the JSON value to an integer
341+ $ jsonExpression = is_int ($ value )
342+ # just incase: ? "CAST(JSON_EXTRACT($column, '$.$jsonKey') AS UNSIGNED)";
343+ ? "JSON_EXTRACT( $ column, '$. $ jsonKey') + 0 "
344+ : "JSON_EXTRACT( $ column, '$. $ jsonKey') " ;
345+
346+ $ this ->query = Builder::where ($ this ->query , $ jsonExpression , $ value , $ comparator );
347+ $ this ->bind (...(Builder::$ bindings ));
348+
349+ return $ this ;
350+ }
351+
352+ /**
353+ * Add a JSON where clause with OR comparator to the query
354+ *
355+ * @param string $column The JSON column
356+ * @param string $jsonKey The key within the JSON structure
357+ * @param mixed $value The value to compare against
358+ * @param string $comparator The comparison operator (default '=')
359+ */
360+ public function orWhereJson (string $ column , string $ jsonKey , $ value , string $ comparator = '= ' ): self
361+ {
362+ $ jsonExpression = is_int ($ value )
363+ ? "JSON_EXTRACT( $ column, '$. $ jsonKey') + 0 "
364+ : "JSON_EXTRACT( $ column, '$. $ jsonKey') " ;
365+
366+ $ this ->query = Builder::where ($ this ->query , $ jsonExpression , $ value , $ comparator , 'OR ' );
367+ $ this ->bind (...(Builder::$ bindings ));
368+
369+ return $ this ;
370+ }
371+
372+ /**
373+ * Add a JSON contains clause to the query
374+ *
375+ * @param string $column The JSON column
376+ * @param mixed $value The value to check for
377+ * @param string|null $jsonKey The key within the JSON structure (optional)
378+ */
379+ public function whereJsonContains (string $ column , $ value , ?string $ jsonKey = null ): self
380+ {
381+ $ jsonValue = json_encode ($ value , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
382+
383+ $ jsonExpression = $ jsonKey
384+ ? "JSON_CONTAINS( $ column, ?, '$. $ jsonKey') "
385+ : "JSON_CONTAINS( $ column, ?) " ;
386+
387+
388+ Builder::$ bindings [] = $ jsonValue ;
389+ $ this ->query = Builder::where ($ this ->query , $ jsonExpression , 1 , '= ' );
390+ $ this ->bind (...(Builder::$ bindings ));
391+
392+ return $ this ;
393+ }
394+
395+ /**
396+ * Add a JSON contains clause with OR comparator to the query
397+ *
398+ * @param string $column The JSON column
399+ * @param mixed $value The value to check for
400+ * @param string|null $jsonKey The key within the JSON structure (optional)
401+ */
402+ public function orWhereJsonContains (string $ column , $ value , ?string $ jsonKey = null ): self
403+ {
404+ $ jsonValue = json_encode ($ value , JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
405+
406+ $ jsonExpression = $ jsonKey
407+ ? "JSON_CONTAINS( $ column, ?, '$. $ jsonKey') "
408+ : "JSON_CONTAINS( $ column, ?) " ;
409+
410+ Builder::$ bindings [] = $ jsonValue ;
411+ $ this ->query = Builder::where ($ this ->query , $ jsonExpression , 1 , '= ' , 'OR ' );
412+ $ this ->bind (...(Builder::$ bindings ));
413+
414+ return $ this ;
415+ }
416+
330417 /**
331418 * Fetch current query with all related data
332419 *
0 commit comments