mercredi 28 décembre 2016

Return specific value from a table to validate with in_array using Laravel

I need to return an array with the key "polls" and "polls_voted". The value of polls_voted needs to be all polls in which there is an answer.

I need to return this array to my view and make this work :

{!! link_to_route('poll.edit', 'Modifier', [$poll->id], ['class' => 'btn btn-warning btn-block' . (in_array($poll->question, $polls_voted)? ' disabled' : '')]) !!}

So i've done the following :

    $polls = Poll::paginate($n);
    $polls_answered = Poll::has('answers')->get();

    $polls_voted = [];

    foreach ($polls_answered as $poll) {
        array_push($polls_voted, $poll->question);
    }

    return compact('polls','polls_voted');

This works but I'm quite sure that there something way easier to do with eloquent or querybuilder.

Any idea ?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2htlwwZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire