如何从刀片中雄辩的过滤器中提取数据

时间:2019-07-03 11:36:14

标签: laravel laravel-blade laravel-5.8

我需要能够为每个循环在a之外提取类别标题,但我不知道如何定义变量

我可以在已经定义好循环的情况下从循环内部进行操作,但是我可以在不需要的循环的每次迭代中获得它。到目前为止,我的代码是

@extends('layouts.games')

@section('content')

<h1>{{TITLE TO GO HERE}}</h1>
<div class="row saleGames">
  @foreach($allGames as $game)
  <div class="col-3"><img height="50" src="{{$game->image ? $game->image->file : 'http://placehold.it/400x400'}}" class="buytItems"></td><br>Price £10<br><br><button class="btn btn-success">Add to Basket</button></div>
  @endforeach
</div>
@endsection

我需要用数据库标题替换TITLE TO GO HERE,但我不知道如何定义它以供循环外使用

  public function show(Categories $category)
    {
      $allGames = Games::where('categories_id', $category->id)->get();      
      return view('games', compact('allGames'));
    }

2 个答案:

答案 0 :(得分:1)

将您的代码更改为以下代码:

public function show(Categories $category)
    {
      $allGames = Games::where('categories_id', $category->id)->get();
      $title = $catgeory->title // this will be name of the field you want to display in your view ;

      return view('games', compact('allGames', 'title'));
    }

然后在刀片文件中:

@extends('layouts.games')

@section('content')

<h1>{{$title}}</h1>
<div class="row saleGames">
  @foreach($allGames as $game)
  <div class="col-3"><img height="50" src="{{$game->image ? $game->image->file : 'http://placehold.it/400x400'}}" class="buytItems"></td><br>Price £10<br><br><button class="btn btn-success">Add to Basket</button></div>
  @endforeach
</div>
@endsection

答案 1 :(得分:1)

将类别添加到紧凑数组

public function show(Categories $category)
{
  $allGames = Games::where('categories_id', $category->id)->get();      
  return view('games', compact('allGames','category));
}

然后用刀片式服务器

<h1>{{ $category->name}}</h1>