php - Laravel 5: in_array() expects parameter 2 to be array, object given -
i trying pass array logged-in user's votes on posts controller view keep getting error
in_array() expects parameter 2 array, object given (view: c:\xampp\htdocs\laravel-5\resources\views\subreddit\show.blade.php)
i have tried use lists()
got error instead missing argument 1 illuminate\database\eloquent\builder::lists()
using lists('post_id')
returns same error in title.
controller
public function show(subreddit $subreddit) { $posts = subreddit::findorfail($subreddit->id)->posts()->get(); $votes = auth::user()->votes()->get(); //i have tried lists() return view('subreddit/show') ->with('subreddit', $subreddit) ->with('posts', $posts) ->with('votes', $votes); }
view
@foreach($posts $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() !!}
you need convert collection array. append toarray()
query below, 'toarray()' converts collection plain php array:
$votes = auth::user()->votes()->get()->toarray();
hope help.
Comments
Post a Comment