Laravel 5.4:避免在刀片服务器和数据库查询中使用html

时间:2017-05-13 15:04:07

标签: php laravel blade

我遇到了一个无法解决的问题。我有一个foreach,每次在数据库中找到值时都会打印出HTML,这一切都有效。 但是,我想避免将html放在controller.php文件中。

我做的那一刻:

$html_console='';
if($article->id_game > '0'){
  $prel_console = \DB::table('info_game')
      ->where('id_game', '=', $article->id_game)
      ->get();
  foreach($prel_console as $name_console)
  {
    $name_console_game = \DB::table('console')
        ->where('id', '=', $name_console->id_console)
        ->first();

    $html_console.='<span class="label">'. $name_console_game->abb_cat.'</span>' ;
  }
}

在刀片中:

{!! $html_console !!}

我试图在刀片中执行此操作:

@foreach ($prel_console as $name_console)
  <span class="label margin-top-5 font-size-10">{{ $name_console_game->abb_cat }}</span>
@endforeach

如果我将foreach放入刀片中,我该如何处理查询&#34; name_console_game&#34;

1 个答案:

答案 0 :(得分:0)

如果info_game表格应该有InfoGame模型,console表格带有Console模型,那么您可以执行以下操作:

控制器:

public function someMethod() 
{
    // assuming that you already have an $article object

    $infoGame = InfoGame::where('id_game', $article->id_game)->get();

    return view('some.view', compact('infoGame'));
}

查看位置views/some/view/blade.php

@foreach($infoGame->console as $name_console_game)

    <span>{{ $name_console_game->abb_cat }}</span>

@endforeach