@@ -319,6 +319,56 @@ public function whereSubmissionContains($field, $string)
319319 }
320320 }
321321
322+ /**
323+ * Lista as submissões com filtro
324+ *
325+ * @param string $field Nome do campo do json dentro de data a ser filtrado
326+ * @param string $operator Operador de comparação. Suporta: contains, =, ==, !=, empty, not_empty
327+ * @param mixed $value Valor a ser comparado. Pode ser string, array ou null
328+ * @return \Illuminate\Database\Eloquent\Collection
329+ */
330+ public function filterSubmissionByField ($ field , $ operator , $ value = null )
331+ {
332+ $ jsonField = "data-> $ field " ;
333+
334+ $ query = new FormSubmission ;
335+ if ($ this ->name ) {
336+ $ query ::where ('form_definition_id ' , $ this ->getDefinition ($ this ->name )->id );
337+ }
338+ if ($ this ->key ) {
339+ $ query ::where ('key ' , $ this ->key );
340+ }
341+
342+ switch ($ operator ) {
343+ case 'contains ' :
344+ // valor dentro do JSON (array ou string)
345+ return $ query ::whereJsonContains ($ jsonField , (string ) $ value )->get ();
346+
347+ case '= ' :
348+ case '== ' :
349+ return $ query ::where ($ jsonField , $ value )->get ();
350+
351+ case '!= ' :
352+ return $ query ::where ($ jsonField , '!= ' , $ value )->get ();
353+
354+ case 'empty ' :
355+ return $ query ::where (function ($ query ) use ($ jsonField ) {
356+ $ query ->whereNull ($ jsonField )
357+ ->orWhere ($ jsonField , '' );
358+ })->get ();
359+
360+ case 'not_empty ' :
361+ return $ query ::where (function ($ query ) use ($ jsonField ) {
362+ $ query ->whereNotNull ($ jsonField )
363+ ->where ($ jsonField , '!= ' , '' );
364+ })->get ();
365+
366+ default :
367+ throw new \InvalidArgumentException ("Operador ' $ operator' não suportado. " );
368+ }
369+ }
370+
371+
322372 /**
323373 * Get a form submission by id
324374 */
0 commit comments