【问题标题】:Included blade template repeating in each foreach loop iteration在每个 foreach 循环迭代中包含重复的刀片模板
【发布时间】:2015-06-21 05:26:49
【问题描述】:

我在 Laravel 中使用 Blade 模板,我正在尝试使用 @foreach 循环来显示通知。问题是如果我说 10 个通知,第一个通知会重复 10 次。

输出每个通知的代码:

@foreach ( Auth::user()->unreadNotifications as $notification )

    {{ $notification->type->web_template }}
    {{ $notification->id }}
    @include($notification->type->web_template)

@endforeach

web_template 将输出模板的路径:notifications.web.user_alert

对于循环的每次迭代,{{ $notification->type->web_template }}{{ $notification->id }} 将输出它们应该输出的内容,但 @include($notification->type->web_template) 每次只会输出第一个通知。

所以输出看起来像:


156 notification.web.new_message

您收到了乔的新消息。


154 notification.web.user_alert

您收到了乔的新消息。


145 notification.web.new_like

您收到了乔的新消息。


我认为这可能是某种缓存问题,但我找不到有同样问题的人。

有什么想法吗?

更新:添加一些代码

通知视图示例:

@extends('layouts.notification-wrapper')

@section('url', url('jobs/'.$notification->job_id.'#highlight'.$notification->bid_id))

@section('image', '/assets/img/no-photo.jpg')

@section('header', $notification->collector->firstname . ' ' . $notification->collector->secondname)

@section('description')
has placed a bid of €{{ number_format($notification->bid()->withTrashed()->first()->amount,0) }} on your job.
@stop

通知包装器:

<li @if(!$notification->read) 
    class="unread" 
    @endif>
    <a href="@yield('url')" data-id="{{ $notification->id }}">
        <div class="pull-left">
            <img src="@yield('image')" class="img-circle" alt="user image">
        </div>
        <h4>
            @yield('header')
            <small><i class="fa fa-clock-o"></i> {{ $notification->created_at->diffForHumans()}}</small>
        </h4>
        <p> @yield('description')</p>
    </a>
</li>

【问题讨论】:

  • 您不应该将通知传递给包含吗? @include($notification-&gt;type-&gt;web_template, ['notification' =&gt; $notification])
  • 我更改了它,但它仍在重复相同的模板。
  • 视图是什么样的?请把它放在问题中。
  • 添加了示例视图及其包装器。
  • 感谢帮助但找到了答案:stackoverflow.com/questions/18659615/…

标签: php laravel foreach blade


【解决方案1】:

回答我自己的问题!

找到这个:Laravel Blade Templates Section Repeated / cache error

基本上无论它以何种方式工作,我都需要在循环和使用@yield时覆盖我的部分...我想。所以我需要在我的观点中将@stop 替换为@overwrite

【讨论】:

    猜你喜欢
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2018-04-04
    • 2019-11-28
    • 1970-01-01
    • 2019-10-11
    • 2016-07-28
    相关资源
    最近更新 更多