【发布时间】:2015-12-17 09:30:48
【问题描述】:
我正在尝试将一个包含登录用户对帖子的投票的数组从控制器传递到视图,但我不断收到此错误
in_array() 期望参数 2 是数组,给定对象(查看: C:\xampp\htdocs\laravel-5\resources\views\subreddit\show.blade.php)
我尝试使用lists(),但我得到了这个错误而不是Missing argument 1 for Illuminate\Database\Eloquent\Builder::lists()
使用lists('post_id') 在标题中返回相同的错误。
控制器
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);
}
查看
@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() !!}
【问题讨论】:
-
仅供参考,您可以将
Auth::user()->votes()->get()更改为Auth::user()->votes和Subreddit::findOrFail($subreddit->id)->posts()->get()更改为Subreddit::findOrFail($subreddit->id)->posts。
标签: php arrays laravel laravel-5