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

ilhamzacky's avatar

Laravel Query Value Need To Explode

Hi all , I have a laravel Query, where it has a value , i need to explode the vlaue and write a where condition. Her is the code

 dd($appointments->whereIn([explode(' ','preference->appoint_info->location_dropdown')],'ssss')->get());

Can some one help me with these Thanks

0 likes
20 replies
Sinnbeck's avatar

What is that supposed to end with? Like how is the query supposed to look?

ilhamzacky's avatar

@Sinnbeck

^ Illuminate\Support\Collection {#1861 ▼
  #items: array:68 [▼
    0 => {#1851 ▼
      +"id": "a3f81160-81fa-11eb-bc69-273474788a80"
      +"first_name": "j"
      +"phone": "999999999999"
      +"email": "[email protected]"
      +"status_code": null
      +"state": 0
      +"preference": "{"userinfo": {"age": "29", "dob": "1992-02-01", "dob_": {"day": "01", "year": "1992", "month": "02"}, "town": "m", "email": "[email protected]", "fname": "j", "lname": "j", "mname": "j", "_token": "TMsAYroACMUYWVrxBepWc1zVDBwZuM9Moc3740Zc", "action": "participant", "gender": "Male", "postcode": "m", "ethnicity": "Indian", "street_no": "m", "home_phone": "999999999999", "postal_code": "j", "street_name": "m", "mobile_phone": "999999999999"}, "enumeration": "false", "appoint_info": {"_token": "TMsAYroACMUYWVrxBepWc1zVDBwZuM9Moc3740Zc", "any_day": "on", "any_time": "on", "location_dropdown": "norwood_hall"}} ◀"
      +"user_id": "a3f81160-81fa-11eb-bc69-273474788a80"
Sinnbeck's avatar

@ilhamzacky That is the dump of some sort of data.

Try writing the actual query you would expect to be written

Like

$appointments->whereIn('ssss', ['appoint_info'. 'location_dropdown'])>get();
ilhamzacky's avatar

@Sinnbeck preference->appoint_info->location_dropdown - this is the column name , i need to explode the column and == to a string

Sinnbeck's avatar

Btw. Is supposed to be a lookup on a json column?

ilhamzacky's avatar

@Sinnbeck yes its a json column, it comes with a long text , i need to get only the name before a ',', and add a where close to filter

Sinnbeck's avatar

@ilhamzacky so it's an array in that column? Not an object. Don't think you can search that.

Can you show an example of the data in a row?

You should consider moving the data to a seperate table to make it easier to work with. If you store an array you should only use it for getting the whole thing

ilhamzacky's avatar

@Sinnbeck You should consider moving the data to a seperate table to make it easier to work with. - actually it was an old implementation , there were several records already been stored,

ilhamzacky's avatar

@Sinnbeck

+"id": "a3f81160-81fa-11eb-bc69-273474788a80" 
+"preference": "{"userinfo": {"dob_": {"day": "01", "year": "1992", "month": "02"}, "email": "[email protected]", "fname": "j", "lname": "j", "mname": "j", "_token": "TMsAYroACMUYWVrxBepWc1zVDBwZuM9Moc3740Zc", "action": "participant", "gender": "Male", "postcode": "m", "ethnicity": "Indian", "street_no": "m", "home_phone": "999999999999", "postal_code": "j", "street_name": "m"}, "enumeration": "false", "appoint_info": {"_token": "TMsAYroACMUYWVrxBepWc1zVDBwZuM9Moc3740Zc", "any_day": "on", "any_time": "on", "location_dropdown": "norwood_hall"}} ◀"
Sinnbeck's avatar

@ilhamzacky so this part is the json column ? Sorry it is hard to read. Maybe just copy/paste from your database manager

"preference": "{"userinfo": {"dob_": {"day": "01", "year": "1992", "month": "02"}, "email": "[email protected]", "fname": "j", "lname": "j", "mname": "j", "_token": "TMsAYroACMUYWVrxBepWc1zVDBwZuM9Moc3740Zc", "action": "participant", "gender": "Male", "postcode": "m", "ethnicity": "Indian", "street_no": "m", "home_phone": "999999999999", "postal_code": "j", "street_name": "m"}, "enumeration": "false", "appoint_info": {"_token": "TMsAYroACMUYWVrxBepWc1zVDBwZuM9Moc3740Zc", "any_day": "on", "any_time": "on", "location_dropdown": "norwood_hall 
ilhamzacky's avatar

@Sinnbeck yes , on the location drop down , it comes with a long text like -norwood_hall, xxxx xxxxx xxxxx xxxx so i wont to explode and get only the norwood hall and add a where closure to filter it

Sinnbeck's avatar

@ilhamzacky so maybe something like this? Unless location_dropdown is an array, then try the third

($appointments->where('preference->appoint_info->location_dropdown', 'LIKE' '%ssss%')->get());

//or

($appointments->whereIn('preference->appoint_info->location_dropdown', ['ssss'])->get());

//or

$appointments->whereJsonContains('preference->appoint_info->location_dropdown ', 'ssss') ->get(); 
Sinnbeck's avatar

@ilhamzacky personally, if I had to make it searchable I would look into migration all the data to seperate tables

1 like

Please or to participate in this conversation.