【发布时间】: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->type->web_template, ['notification' => $notification]) -
我更改了它,但它仍在重复相同的模板。
-
视图是什么样的?请把它放在问题中。
-
添加了示例视图及其包装器。
-
感谢帮助但找到了答案:stackoverflow.com/questions/18659615/…