Have you tried with query builder. Also search some post from @tray2 where he discusses not storing json in a field.
I generally agree, unless it's a very short simple json, nothing nested.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello Laracasts forum, I have a column in MySQL database that is storing multiple IDs that crosspond to different records in another table. What I have tried to do is instead of making a pivot table(if that terminology is right) I have stored all the IDs in a column and cast it into an array in Model.
This is what is being stored in the MySQL column. and Column name is belongs_to_sport = ["1", "2", "3"]
Now I want to query in Eloquent to filter these records such as
$News = News::whereJsonContains('belongs_to_sport, 1)->get(); This Query should give me the above record.
$News = News::whereJsonContains('belongs_to_sport, 5)->get(); This Query should not give me the above record.
Does that make sense? Is that even possible what I am trying to achieve here? What I am doing wrong?
I got it to work. I was not adding the " " around the variable that is being passed to the query.
Old Query (Not working) $news = DB::table('news')->whereJsonContains('n_belongs_to_Sports', $sport_cateory_id)->get(); dd($news);
New Query(Working) $news = DB::table('news')->whereJsonContains('n_belongs_to_Sports', "$sport_cateory_id")->get(); dd($news);
Difference $sport_cateory_id needs to be "$sport_cateory_id"
Thank you for your Help. Much appreciate it @jlrdw
Please or to participate in this conversation.