Php 具有视图组件和一对多关系的Laravel视图生成器
简而言之,我想知道是否有比我当前的设置更好的方法来使用视图生成器 我有一个带有以下Php 具有视图组件和一对多关系的Laravel视图生成器,php,laravel,laravel-5,Php,Laravel,Laravel 5,简而言之,我想知道是否有比我当前的设置更好的方法来使用视图生成器 我有一个带有以下boot()code的Composer服务提供程序: view()->composer(['components.carousel'], function ($view) { $carousels = Carousel::with('slides')->get(); $view->with(compact('carousels')); }); 我的组件非常简单: <styl
boot()
code的Composer服务提供程序
:
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>
困扰我的是这一块:
@php
$carousel = $carousels->filter(function ($carousel, $key) use ($name) {
return ($carousel->name == $name);
});
@endphp
我返回每个带幻灯片关系的旋转木马,然后过滤旋转木马,只使用我需要的一个carousel->slides
关系。有没有办法让视图生成器知道我需要的旋转木马的名称?还是有更好的方法来解决这个问题
谢谢,您想将此php代码排除在视图之外。这样做更好。有两种选择。您可以将其添加到模型中,让我们以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 dump autoload,这些函数在视图中的任何位置都可以使用,@php$carousel=carousel($carousels,'Super-Awesome carousel')@endphp
由于您经常使用它,我会选择functions.php选项。您应该在控制器中创建$carousel对象并将其传递。您可以从您应该看到的视图中移开的内容 您可以在加载视图的控制器中获取单个旋转木马,然后将行和关系的变量传递给组件?我可以,但是我必须在可能使用组件的每个控制器中编写该代码。我希望无论使用哪个视图,组件都可以使用这些数据。然后,您可以将雄辩的查询移动到组件本身内部,并使用$name变量进行查询,而不是过滤整个集合。
<?php
if ( ! function_exists("Carousel")) {
function Carousel($carousels, $name) {
return $carousels->filter(function ($carousel, $key) use ($name) {
return ($carousel->name == $name);
}
}
}
"files": [
"app/Functions/functions.php"
],