Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 8.x Where Json Search In Array

My DB column field ("colors") contains an array structured like this:

["1","2","3"]

My current query look like this, simplified for better example:

$value = '1';

$cards = Card::query()
->where('colors', 'like', '%' . $value . '%')
->get();

The problem is, it fetches data from other columns, for example ["5","8","13"] will match "13" instead of the strict $value, which should be "1".

How do I write this query the correct way?

like image 386
devopix Avatar asked Oct 14 '25 08:10

devopix


1 Answers

used whereJsonContains Method here

value like this then do like that ["1","2","3"]

$cards = Card::query()
->whereJsonContains('colors', '1')
->get();

if value like that [1,2,3] then used like that

$cards = Card::query()
    ->whereJsonContains('colors', 1)
    ->get();
like image 92
Jignesh Joisar Avatar answered Oct 16 '25 22:10

Jignesh Joisar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!