Owe*_*agh 4 php foreach laravel laravel-blade
我正在 Laravel 中使用 Blade 模板,并且尝试使用循环@foreach来显示通知。问题是,如果我有 10 个通知,第一个通知会重复 10 次。
输出每个通知的代码:
\n\n@foreach ( Auth::user()->unreadNotifications as $notification )\n\n {{ $notification->type->web_template }}\n {{ $notification->id }}\n @include($notification->type->web_template)\n\n@endforeach\nRun Code Online (Sandbox Code Playgroud)\n\nweb_template将输出模板的路径:notifications.web.user_alert
对于循环的每次迭代,{{ $notification->type->web_template }}和{{ $notification->id }}都会输出它们应该输出的内容,但@include($notification->type->web_template)每次只会输出第一个通知。
所以输出将如下所示:
\n\n156 通知.web.new_message
\n\n您有一条来自乔的新消息。
\n\n154 通知.web.user_alert
\n\n您有一条来自乔的新消息。
\n\n145 通知.web.new_like
\n\n您有一条来自乔的新消息。
\n\n我认为这可能是某种缓存问题,但我找不到有同样问题的人。
\n\n有任何想法吗?
\n\n更新:添加一些代码
\n\n通知视图示例:
\n\n@extends(\'layouts.notification-wrapper\')\n\n@section(\'url\', url(\'jobs/\'.$notification->job_id.\'#highlight\'.$notification->bid_id))\n\n@section(\'image\', \'/assets/img/no-photo.jpg\')\n\n@section(\'header\', $notification->collector->firstname . \' \' . $notification->collector->secondname)\n\n@section(\'description\')\nhas placed a bid of \xe2\x82\xac{{ number_format($notification->bid()->withTrashed()->first()->amount,0) }} on your job.\n@stop\nRun Code Online (Sandbox Code Playgroud)\n\n通知包装器:
\n\n<li @if(!$notification->read) \n class="unread" \n @endif>\n <a href="@yield(\'url\')" data-id="{{ $notification->id }}">\n <div class="pull-left">\n <img src="@yield(\'image\')" class="img-circle" alt="user image">\n </div>\n <h4>\n @yield(\'header\')\n <small><i class="fa fa-clock-o"></i> {{ $notification->created_at->diffForHumans()}}</small>\n </h4>\n <p> @yield(\'description\')</p>\n </a>\n</li>\nRun Code Online (Sandbox Code Playgroud)\n
回答我自己的问题!
发现这个:Laravel Blade 模板部分重复/缓存错误
@yield基本上,无论它的工作方式如何,我认为在循环和使用时都需要覆盖我的部分。所以我的观点需要替换@stop为。@overwrite