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

Wednesday, November 16, 2022

[FIXED] How to retrieve Json data type data in SQL using a where condition in LARAVEL

 November 16, 2022     eloquent, laravel, laravel-5, laravel-6, laravel-7     No comments   

Issue

Question : How to retrieve Json data type data in SQL using a where condition in LARAVEL?

I want to display all the order that contains order->Product->user->id === 1

{
"currentUserID": 1,
"currentUserName": "Mohamed Naalir",
"order": [
    {
        "id": 26,
        "Product": [
            {
                "id": 4,
                "name": "Araliya Rice",
                "desription": "Araliya Rice",
                "salePrice": 500,
                "category": "Rice",
                "user": {
                    "id": 1,
                    "name": "Mohamed Naalir",
                }
            }
        ],
    },
    {
        "id": 27,
        "Product": [
            {
                "id": 2,
                "name": "white sugar",
                "desription": "aaa",
                "salePrice": 100,
                "category": "Sugar",
                "user": {
                    "id": 5,
                    "name": "Mohamed Sharaf",
                }
            }
        ],
    }
]

}


Solution

json where clauses

 $orders = DB::table('orders')
            ->whereJsonContains('Product', [['user' => ['id' => 1]]])
            ->get();


Answered By - Mohamed Raza
Answer Checked By - Robin (PHPFixing Admin)
  • 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