1

It seems like this should be pretty simple to find the answer to but apparently not for me. I need to query a count of records where the field called bullets contains an array with a length of 5. Here is what I currently have but it's checking for character length of the field itself(I believe), not of an array. Can anyone help me out please?

$vcount = Variation::where('user_id', user_id)->whereRaw('LENGTH(bullets) = 5')->count();
2
  • Try using the JSON_LENGTH sql function to get the number of elements in the array. Commented Jun 23, 2022 at 23:15
  • @pmcpimentel Thanks for the suggestion but this didn't work for me (Or maybe my syntax was wrong). I ended up finding whereJsonLength() in this thread stackoverflow.com/questions/50820839/… Will post the answer below Commented Jun 24, 2022 at 14:00

1 Answer 1

1

thanks to this thread Laravel 5.6. JSONb column. Count array

I found whereJsonLength() that worked for my use case:

$vcount = Variation::where('user_id', user_id)->whereJsonLength('bullets', '=', 5)->count();
Sign up to request clarification or add additional context in comments.

Comments

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.