具有视图组件和一对多关系的Laravel View Composer

时间:2018-04-01 00:06:48

标签: php laravel laravel-5

简而言之,我想知道是否有比使用当前设置更好的方式使用视图编辑器。

我有一个ComposerServiceProvider,其中包含以下boot()代码:

view()->composer(['components.carousel'], function ($view) {
    $carousels = Carousel::with('slides')->get();
    $view->with(compact('carousels'));
});

我的组件非常简单:

<style type="text/css">
  .carousel-item {
    height: 100vh;
    min-height: 300px;
    background: no-repeat center center scroll;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
  }
  .carousel-caption {
    background-color: rgba(0,0,0,0.1);
    top: 40%;
    bottom: unset;
  }
</style>

@php
  $carousel = $carousels->filter(function ($carousel, $key) use ($name) {
    return ($carousel->name == $name);
  });
@endphp

<header>
  <div id="carouselIndicators" class="carousel slide" data-ride="carousel">
    <ol class="carousel-indicators">
      @foreach ($carousel[0]->slides as $slide)
        <li data-target="#carouselIndicators"
            data-slide-to="{{ $loop->index }}"
            class="{{ $loop->index == 0 ? 'active' : '' }}"></li>
      @endforeach
    </ol>
    <div class="carousel-inner" role="listbox">
      @foreach ($carousel[0]->slides as $slide)
        <div class="carousel-item {{ $loop->index == 0 ? 'active' : '' }}"
             style="background-image: url('{{ Storage::url($slide->image) }}')">

          @if ($slide->title || $slide->description || $slide->link)
            <div class="carousel-caption d-none d-md-block">
              <h3>{{ $slide->title }}</h3>
              <p>{{ $slide->description }}</p>
              <a class="btn btn-primary btn-sm" href="{{ $slide->link }}">Learn More</a>
            </div>
          @endif

        </div>
      @endforeach
    </div>
    <a class="carousel-control-prev" href="#carouselIndicators" role="button" data-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="sr-only">Previous</span>
    </a>
    <a class="carousel-control-next" href="#carouselIndicators" role="button" data-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="sr-only">Next</span>
    </a>
  </div>
</header>

使用组件:

@component('components.carousel', [
    'name' => 'Super Awesome Carousel'
])
@endcomponent

困扰我的是这件作品:

@php
  $carousel = $carousels->filter(function ($carousel, $key) use ($name) {
    return ($carousel->name == $name);
  });
@endphp

我将使用幻灯片关系返回每个轮播,然后过滤轮播并仅使用我需要的一个carousel->slides关系。有没有办法让视图作曲家知道我需要的旋转木马的名字?或者有更好的方法来解决这个问题吗?

谢谢,

1 个答案:

答案 0 :(得分:1)

您希望从您的观看中排除此PHP代码。最好这样做。有2个选项。您可以将其添加到模型中,让我们以Picture.php为例。选择最常用的模型。

public static function Carousel($carousels, $name) {
   return $carousels->filter(function ($carousel, $key) use ($name) {
      return ($carousel->name == $name);
   });
 }

在视图调用@php $carousel = Picture::Carousel($carousels, 'Super Awesome Carousel') @endphp中,您可以删除components.carousel组件。

另一种方法是在app\Functions\functions.php

中创建自己的自定义函数
<?php
if ( ! function_exists("Carousel")) {
function Carousel($carousels, $name) {
  return $carousels->filter(function ($carousel, $key) use ($name) {
      return ($carousel->name == $name);
    }
  }
}

composer.json中添加以下行:

"files": [
  "app/Functions/functions.php"
],

运行composer dump-autoload,这些功能在视图中的任何位置都可用@php $carousel = Carousel($carousels, 'Super Awesome Carousel') @endphp

当你经常使用它时,我会选择functions.php选项。您应该在控制器中创建$ carousel对象并将其传递。什么你可以摆脱你应该的意见。