dtddjq9637 2019-05-06 06:52
浏览 105
已采纳

如何在laravel 5.7中获得json值?

I have two tables in a database, package and categories.

package has a field category_id that contains data in this form ["1","11"] and shows categories data in the form of a checkbox.

Now, I want all the data from package where category_id equal to categories id.

Here's my code :

public function archive_packages(Request $request, $slug){
    $title='Archive Packages';
    $para='';
    $slugs='archive-packages';
    $categoryRow = Category::where('slug',$slug)->first();
    $categoryID = $categoryRow->id;
    $package = Packages::whereRaw('JSON_CONTAINS(category_id, \'["11"]\')')->get();
    dd($package);
    return view('pages.archive-packages',compact('title','para','slugs','package'));
}

I'm getting the following error :

SQLSTATE[42000]: Syntax error or access violation: 1305 FUNCTION
pre.JSON_CONTAINS does not exist (SQL: select * from `packages` where
JSON_CONTAINS(category_id, '["11"]'))

  • 写回答

3条回答 默认 最新

  • douwo6738 2019-05-06 07:08
    关注

    There is a laravel method to do this, more details here

    So please try:

    Packages::whereJsonContains('category_id', '11')->get();
    

    If your mysql version does not support json_contains and you cannot upgrade it, then try this query:

    Packages::where('category_id', 'like', '%"11"%');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog