Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Respect's avatar

how to use where query a in casted array field

Hello friends, how to use where query a in casted array field for EXAMPLE

  • in model
 protected $casts = [
        'supplier_ids' => 'array',
 ];
  • Data Stored in Database table field in this format supplier_ids [1,2,3,4]
// db table field data stored like thats
[1,2,3,4]
[1,3,5,7]
  • AND NOW I need LIST ALL DATA query only if supplier_ids field where casted array = 3 IN one of them FOR EXAMPLE
$currentSupplierId = 3;
$orders = Order::->where('supplier_ids', $currentSupplierId)->get();

** HOW TO DO THIS QUERY , THANKS FOR TIME

0 likes
9 replies
Shivamyadav's avatar

Try this ''' $currentSupplierId = 3; $orders = Order::where('supplier_ids', 'LIKE', '%"'.$currentSupplierId.'"%')->get(); ''''

1 like
vincent15000's avatar

@Shivamyadav It's not a good idea because in the database it's a JSON format and in your suggestion you consider the field as a simple string, but it's not a simple string.

Your query will find any order which contains 3, for example 3 as well as 35 or 53.

1 like
Respect's avatar

@Shivamyadav Thanks so much for answer - like will show like data (search) whereJsonContains() worked

1 like
jatinkumar's avatar

Data is saved in JSON format in the database. So json where clauses should be used as suggested by @vincent15000 above.

1 like
Respect's avatar

@Tray2 Thanks so much for answer and advice i have to because order from maultiaple shops

1 like
Tray2's avatar

@Respect I got one word for that, and that is BULLSHIT, there are always a proper way of doing it, and a json column is seldom the proper way.

1 like

Please or to participate in this conversation.