php Laravel 5:in_array() 期望参数 2 是数组,给定的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32682400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:06:48  来源:igfitidea点击:

Laravel 5: in_array() expects parameter 2 to be array, object given

phparrayslaravellaravel-5

提问by Halnex

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)

in_array() 期望参数 2 是数组,给定对象(视图: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()

我曾尝试使用,lists()但出现此错误Missing argument 1 for Illuminate\Database\Eloquent\Builder::lists()

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

使用lists('post_id')在标题中返回相同的错误。

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() !!}

回答by mdamia

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:

您需要将您的集合转换为数组。只需toArray()像下面那样附加到您的查询中,因为 'toArray()' 将集合转换为普通的 PHP 数组:

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

Hope this help.

希望这有帮助。

回答by Paul Spiegel

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

您可以像其他建议的使用toArray()方法一样将集合转换为数组。但我宁愿改变

in_array($post->id, $votes)

to

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

Update

更新

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

以上仅适用$votes于 ID 的集合。例如。当你使用

$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 $votesis a collection of Voteobjects, and you only want to search in the post_idcolumn/attribute.

因为$votesVote对象的集合,您只想在post_id列/属性中进行搜索。

回答by Suren

I think pluck will solve your problem.

我认为 pluck 会解决你的问题。

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