2
I’m making a foreach
which brings the following database:
$notificacoes = Notificacoes::where('user_id', $id)
->where('lido', false)
->orderBy('created_at', 'desc')
->get();
$notificacao->load('causa','causa.cliente','causa.cliente.user','causa.mensagem');
return $notificacoes;
In Blade I’m execuntado the following foreach
@foreach($notificacoes as $notificacao)
@if($notificacao->tipo == 'NOVA MENSAGEM')
<div class="comment-widgets m-b-20">
<!-- Comment Row -->
<div class="d-flex flex-row comment-row">
<div class="comment-text w-100">
<h5><a href="#">{{$notificacao->causa->cliente->user->nome}}</a> te enviou uma nova mensagem na causa:</h5>
<div class="comment-footer">
<p class="m-b-5 m-t-10">{{$notificacao->causa->assunto}}</p>
<span class="date">{{date('d/m/Y', strtotime($notificacao->created_at))}} as {{date('H:i:s', strtotime($notificacao->created_at))}} </span>
</div>
</div>
</div>
<!-- Comment Row -->
</div>
@endif
@endforeach
Yet when it comes to $notificacao->causa->mensagem
I bring a array
and need only the last message sent. Follow the photo of dump
and die
:
Does anyone know how to get any value within the last index of array
within the foreach
?
Something like the function
end()
?– Woss
I saw something about end(), however I could not implement since within the last Dice would take the data "message" and "create_at". You would know how to proceed?
– Betini O. Heleno
Or how it’s manageable and it’s not as simple as it could be, maybe it’s
$notificacao->causa->mensagem->last()->created_at
– Woss
I must admit that I did not know last(). However it worked perfectly. Quick and useful solution
– Betini O. Heleno
You should think about loading only the last message and then show! the command quoted above is correct, but, I think the most correct is to bring only what you need!
– novic
If you take your collection just make a Count of it - 1 you will get the position of the last array:
$collection[count($collection) - 1];
– Ivan Ferrer