【问题标题】:How to check value passed with a view inside a component in laravel如何检查laravel组件内视图传递的值
【发布时间】:2021-06-23 02:20:49
【问题描述】:

您好,我是 laravel 新手,遇到了这个问题。

在我的控制器中,我返回了一个带有数组的视图,它运行良好

$cards = array('type' => 'news', 'value' => 'some value here');

return View::make('home')->with('cards', $cards);

在我的 home.blade.php 里面我有这样的东西:

<html>
<head> ... </head>
<body>
@foreach($cards as $card)
    @component('components.card')
        @slot('imageSource')
            {{ $card['imageSource'] }}
        @endslot

        @slot('heading')
            {{ $card['heading'] }}
        @endslot

        @slot('no_of_likes')
            {{ $card['no_of_likes'] }}
        @endslot
    @endcomponent
@endforeach
</body>
</html>

上面的工作也很好。

主要问题在于我的card.blade.php,它看起来像这样:

<div>
    <div class="row">
        <div class="col-md-6"><a href="{{$imageSource}}"><img src="{{$imageSource}}" alt="Error: MD000 (image not found)" class="img-fluid img-thumbnail"></a></div>
        <div class="col-md-6">
            <h3>{{$heading}} </h3>
            <div class="btn-group" role="group" aria-label="Basic example">
                <button type="button" class="btn"><i class="fa fa-thumbs-up text-secondarysecondary" style="font-size: 3em; color: #808080;"></i></button>
                <button type="button" class="btn"><i class="fa fa-thumbs-down text-secondary" style="font-size: 3em; color: #808080;"></i></button>
                <button type="button" class="btn"><i class="fa fa-comment text-secondary" style="font-size: 3em; color: #808080;"></i></button>
            </div>
            <div><strong>{{$no_of_likes}} likes</strong></div>
            
        </div>
        <hr>
    </div>

    <hr class="my-3 border-top" /> <!-- create a horizontal divider that will divide each card -->
</div>

现在的问题是我想在卡片中再添加一个 div,但前提是卡片的类型不是新闻。 而且我不知道如何在组件内部使用 if 语句来检查 $cards['type']

的值

【问题讨论】:

    标签: php laravel laravel-5 laravel-blade


    【解决方案1】:

    调用@component时可以向组件传递额外的数据:

    @component('components.card', ['cardtype' => $card['type']])
    ...
    @endcomponent
    

    然后在你的组件中你可以简单地使用:

    @if ($cardtype != 'news')
    ...
    @endif
    

    Components & Slots

    【讨论】:

    • 谢谢您,先生,这正是我要找的。​​span>
    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 2016-04-17
    • 1970-01-01
    • 2021-11-20
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多