简体   繁体   中英

Laravel Eloquent : Get object from database using JSON column key

I tried to use the following code to retrieve an entire subscription object from my subscription table using the testId column. testId column is delclared as "json" type but in reality, the content of this column is an array with a single string as follow :

 ["51602a95-73d1-4c24-b3b3-eee288b427e4"]

I tried to get the subscription object with this code but ot doesn't work. How can i adpat this piece of code to get the subscription object by searching the value of testId into the array ?

function getSubscriptionByTestId($testId) {
   $subscription = Subscription::where('testId', $testId)->first();
   return $subscription;
}

如果所有值都类似于["51602a95-73d1-4c24-b3b3-eee288b427e4"]具体取决于您的数据库引擎,则可以使用:

$subscription = Subscription::where('testId','like', "%$testId%")->first();

You can try the following, using a raw query. I'm not yet sure how to handle this with Eloquent, but will investigate further.

    return DB::select(
      "SELECT * FROM subscription
      WHERE testId->\"$[0]\" = \"{$testId}\""        
    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM