从表中返回特定值以使用Laravel使用in_array进行验证

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 ?

If you want to get a single column from a set of records, simply use lists method instead of get. lists method takes a column name as a parameter and an array with the values of a given column.

This should do the trick:

$polls_voted = Poll::has('answers')->lists('question');