11

I am trying to pass an array with the logged-in user's votes on the posts from the controller to the view but I keep getting this error

in_array() expects parameter 2 to be array, object given (View: C:\xampp\htdocs\laravel-5\resources\views\subreddit\show.blade.php)

I have tried to use lists() but I got this error instead Missing argument 1 for Illuminate\Database\Eloquent\Builder::lists()

Using lists('post_id') returns the same error in the title.

Controller

public function show(Subreddit $subreddit)
{
    $posts = Subreddit::findOrFail($subreddit->id)->posts()->get();
    $votes = Auth::user()->votes()->get(); //I have also tried lists()

    return view('subreddit/show')
        ->with('subreddit', $subreddit)
        ->with('posts', $posts)
        ->with('votes', $votes);
}

View

@foreach($posts as $post)
    @if ($voted = in_array($post->id, $votes))
      {!! Form::open(['url' => 'votes', 'class' => 'votes']) !!}
    @endif
      <div class="upvote topic" data-post="{{ $post->id }}">
         <a class="upvote vote {{ $voted ? 'voted-on' : '' }}" data-value="1"></a>
         <span class="count">0</span>
         <a class="downvote vote {{ $voted ? 'downvoted-on' : '' }}" data-value="-1"></a>
      </div>
    {!! Form::close() !!}
1
  • FYI, you can change Auth::user()->votes()->get() to just Auth::user()->votes and Subreddit::findOrFail($subreddit->id)->posts()->get() to just Subreddit::findOrFail($subreddit->id)->posts. Commented Sep 20, 2015 at 18:09

3 Answers 3

36

You need to convert your collection to array. Just append toArray() to your query like below, as 'toArray()' converts the collection into a plain PHP array:

 $votes = Auth::user()->votes()->get()->toArray(); 

Hope this help.

Sign up to request clarification or add additional context in comments.

6 Comments

That gave me Call to undefined method Illuminate\Database\Eloquent\Collection::to_array() even though I have included use Illuminate\Database\Eloquent\Collection at the top.
is it toarray() without the underscore?
oops, it's toArray() not to_array(), corrected my answer
Thank you, that worked. Please update your answer so I can select it as the correct one. @ajameswolf can you please elaborate?
So you are only using one column, I assume that is why you were using the lists command. This can be achieved in less steps and cleaner code: $posts = Subreddit::findOrFail($subreddit->id)->posts()->select('post_id')->get(); Untested, but should work.
|
10

You can convert the collection to an array like other suggested using toArray() method. But i'd rather change

in_array($post->id, $votes)

to

$votes->contains($post->id)

Update

The above only works if $votes is a collection of IDs. Eg. when you use

$votes = Auth::user()->votes()->pluck('post_id');

However, with your current controller code you would need to use

$votes->contains('post_id', $post->id);

since $votes is a collection of Vote objects, and you only want to search in the post_id column/attribute.

1 Comment

@if($votes->contains('votes_column_id', $post->id)) Thanks for the advice. For me it only worked by entering the name of the column.
5

I think pluck will solve your problem.

$voteIds = Auth::user()->votes()->get()->pluck('id')->toArray();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.