2017-10-10 54 views
0

我正在尝试构建标签系统。 在我的数据库中,我有表topicstags。标签是一个JSON对象。Laravel在json中查询查询值

我的路线:

Route::get('t/{tag}', '[email protected]')->name('tag'); 

public function tag($tag) 
{ 
    return view('tags.tag', ['tag' => $tag, 'topics' => DB::table('topics')->get()]); 
} 

然后:

@foreach ($topics as $topic) 
    @if (in_array($tag, json_decode($topic->tags, true))) 
    <tr> 
     <td class="col-md-6 col-sm-6"> 
      <a href="#" title=""> 
       <strong>{{ $topic->topic_name }}</strong> 
      </a>  
     </td> 
     <td class="col-md-1 col-sm-1 text-right"> 
      @foreach (json_decode($topic->tags) as $tag) 
       <span class="badge badge-primary">{{ $tag }}</span> 
      @endforeach 
     </td> 
    </tr> 
    @endif 
@endforeach 

我知道这是不是一个完美的解决方案,我需要使用,如果在查询中。我怎样才能做到这一点?提前致谢。 P.S我知道' - > where('topics-> tags','..')

但是我的MySQL比5.7老,我不能使用这些新功能。有什么建议么?

+0

你为什么不能够使用'那里(时它会转换JSON数组)' ? – ggdx

+0

..或者最好的方法是更新mysql? – DisplayName

回答