Laravel 5.4:避免在 blade 中的 Controllers 和 DB 查询中使用 html

Laravel 5.4: Avoid html in the Controllers and DB queries in the 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>' ;
  }
}

在 blade 中:

{!! $html_console !!}

我试图在 blade 中这样做:

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

如果我把 foreach 放在 blade 中,我如何处理查询 "name_console_game"

如果 info_game table 和 console table 之间有一对多关系,应该有 InfoGame 模型和 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