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"]'))

图片转代码服务由CSDN问答提供 功能建议

我在数据库中有两个表, package </ code>和 categories </ code >。</ p>

package </ code>有一个字段 category_id </ code>,其中包含此格式的数据 [“1”,“11”] </ code>并以复选框的形式显示 categories </ code>数据。 </ p>

现在,我想要来自 package </ code>的所有数据,其中 category_id </ code>等于 categories </ code> id </ code>。</ p>

这是我的代码:</ p>

  public function archive_packages(Request $ request,$ slug){
 $ title  ='存档包'; 
 $ para =''; 
 $ slugs ='archive-packages'; 
 $ categoryRow = Category :: where('slug',$ slug) - &gt; first(); \  n $ categoryID = $ categoryRow-&gt; id; 
 $ package = Packages :: whereRaw('JSON_CONTAINS(category_id,\'[“11”] \')') - &gt; get(); 
 dd($  package); 
返回视图('pages.archive-packages',compact('title','para','slugs','package')); 
} 
 </ code> </ pre> \  n 
 

我收到以下错误:</ p>

SQLSTATE [42000]:语法错误或访问冲突:1305 FUNCTION
pre。 JSON_CONTAINS不存在(SQL:select * from`packages`其中
JSON_CONTAINS(category_id,'[“11”]'))</ p> </ blockquote> </ div>

3条回答 默认 最新

相关推荐 更多相似问题