Take the last value of an Array inside a Foreach

Asked

Viewed 210 times

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:

inserir a descrição da imagem aqui

Does anyone know how to get any value within the last index of array within the foreach?

  • 1

    Something like the function end()?

  • 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?

  • 2

    Or how it’s manageable and it’s not as simple as it could be, maybe it’s $notificacao->causa->mensagem->last()->created_at

  • 1

    I must admit that I did not know last(). However it worked perfectly. Quick and useful solution

  • 1

    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!

  • 1

    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];

Show 1 more comment

1 answer

2


Just bring from your database what you need, do a filter loading and limit as follows:

$notificacao = Notificacoes::where('user_id', $id)
    ->where('lido', false)
    ->orderBy('created_at', 'desc')
    ->get();

$notificacao->load('causa','causa.cliente','causa.cliente.user');
$notificacao->load(['causa.mensagem' => function (Builder $query) {
    $query->orderBy('id', 'desc')
          ->limit(1);
}]);

return $notificacoes;

Was placed id for ordering the greater for the lesser and a limit 1 to bring only the last entered record, but, can be placed the field that has reference to this aspect.

Reference: Eloquent: Relationships - Lazy Eager Loading

Browser other questions tagged

You are not signed in. Login or sign up in order to post.