PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 13, 2022

[FIXED] How to sum in a json field that contains an array of objects with Eloquent

 January 13, 2022     laravel, postgresql     No comments   

Issue

[
  {"id":4,"sum":19},
  {"id":3,"sum":9},
  {"id":6,"sum":32}, 
  {"id":2,"sum":32}
]

I'm trying this, but it finds only by 1 value, how to find by sum of "sum" values?

 $query->whereJsonContains('programs', [['sum' => intval($search)]]);

Solution

You may use

//...
->whereRaw(
'JSON_CONTAINS(programs->"$[*].sum", "' . intval($search) . '")')->get();

See related



Answered By - Foued MOUSSI
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing