haizone
4 months ago

how to make where() on Json Filed

Posted 4 months ago by haizone

hi i have a filed in my mysql db

like that:

{"os":{"name":"\u05de\u05e2\u05e8\u05db\u05ea \u05d4\u05e4\u05e2\u05dc\u05d4","value":"android oreo 8.1"},"storage":{"name":"\u05e0\u05e4\u05d7 \u05d0\u05d7\u05e1\u05d5\u05df","value":"64gb \/ 128gb \/ 256gb"},"memory":{"name":"\u05d6\u05d9\u05db\u05e8\u05d5\u05df \u05e2\u05d1\u05d5\u05d3\u05d4","value":"6gb \/ 8gb"},"screen-size":{"name":"\u05d2\u05d5\u05d3\u05dc \u05de\u05e1\u05da","value":""},"front-camera":{"name":"\u05de\u05e6\u05dc\u05de\u05d4 \u05e8\u05d0\u05e9\u05d9\u05ea","value":"12+5 \u05de\u05d2\u05d4-\u05e4\u05d9\u05e7\u05e1\u05dc"},"second-camera":{"name":"\u05de\u05e6\u05dc\u05de\u05d4 \u05de\u05e9\u05e0\u05d9\u05ea","value":"20 \u05de\u05d2\u05d4-\u05e4\u05d9\u05e7\u05e1\u05dc, \u05de\u05e4\u05ea\u05d7 \u05e6\u05de\u05e6\u05dd f\/2.0, \u05d0\u05e0\u05d9\u05de\u05d5\u05d2'\u05d9"},"brand":{"name":"\u05d9\u05e6\u05e8\u05df","value":"xiaomi"}}

its look like this (its in hebrew) (u can look in this url http://jsonviewer.stack.hu/ )

that is the result: http://prntscr.com/nlpf3p

i whant to make a filter with this json

from that url: http://localhost:8000/api/category/mobile-phones/products?os=android&memory=6gb-8gb

how i can do that with laravel ?

i try to make somthing like that but isnt work to me:

$ff = $category->products()->whereRaw('JSON_CONTAINS(specfields->os->value, \'["android"]\')')->get();

id somone can help me and how i do with a multi filters like i do in the url "?os=android&memory=6gb-8gb"

many thanks :)

Please sign in or create an account to participate in this conversation.