2017-10-10 98 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

回答