0

Using Laravel to check if a value is in an array stored as comma delimited string, example string stored in mysql table 1,14,15,20,25

So I'm trying to add comma's at the start and end to search appropriately

$query = Profile::whereRaw("',' + cf_profile.sub_category_id + ',' LIKE '%,$sub_category->sub_category_id,%'");

Can anyone let me know where I'm going wrong here, as it's returning no results.

1 Answer 1

2

You can use FIND_IN_SET():

WHERE FIND_IN_SET($sub_category, sub_category_id) > 0
Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately that function is only returning if the exact number is stored and not if it is in the comma delimited string. i.e. it will return a sub_category_id of 2 in mysql table but not if it is 1,15,2 (this will be skipped). I need it so it will find all occurrences, any further suggestions?
@sprintcar78a it is working fine. Check this fiddle. Better create a fiddle if you are still not getting desired o/p.
Ignore that I had $haystack, $needle NOT $needle, $haystack!! Back to the original deleted comment of thank you! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.