Kevin Pimentel
Kevin Pimentel

Reputation: 1916

Laravel View Composer with View Component and One To Many Relationship

In short, I am wondering if there is a better way of using view composers than my current setup.

I have a ComposerServiceProvider with the following boot() code:

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

My component is pretty straightforward:

<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>

To use the component:

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

What is bothering me is this piece:

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

I am returning every carousel WITH the slides relationship and then filtering the carousels and only using the one carousel->slides relationship I need. Is there a way of letting the view composer know the name of the carousel I need? Or is there a better way of approaching this?

Thanks,

Upvotes: 1

Views: 665

Answers (1)

Dimitri Mostrey
Dimitri Mostrey

Reputation: 2355

You want to exclude this php code out of your views. It is better to do that. There are 2 options. You can add it to a Model, let's take Picture.php as an example. Take a model you use most.

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

In the view call @php $carousel = Picture::Carousel($carousels, 'Super Awesome Carousel') @endphp As such, you can drop the components.carousel component.

Another way to do it is by creating your own custom function(s) in 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);
    }
  }
}

In composer.json add this line:

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

Run composer dump-autoload and the functions are available anywhere in your view as @php $carousel = Carousel($carousels, 'Super Awesome Carousel') @endphp

As you are using it so often, I would chose the functions.php option. You should create the $carousel object in your Controllers and pass it on. What you can move away from the views you should.

Upvotes: 1

Related Questions