推送数组中的值并验证

时间:2018-02-22 08:03:46

标签: laravel laravel-5

我需要选择不同的值并将它们推入数组中。 之后,我需要验证变量是否位于数组中。

控制器:

$people = DB::table('checklists')->select('incaricato')->distinct()->get()->toArray();

if (in_array(\Auth::user()->id, $people)) {
    $variable = "yes";
}

错误:

Object of class stdClass could not be converted to int

3 个答案:

答案 0 :(得分:1)

尝试直接使用id,而不是通过user,如下所示:

if(in_array(Auth::id(), $people))

如果它抛出相同的错误,那么转储它并检查它返回的是什么

答案 1 :(得分:0)

解决方案:

$people= \App\Checklist::distinct()->pluck('incaricato')->toArray();

答案 2 :(得分:0)

使用

DB::table('checklists')->distinct()->pluck('incaricato')->toArray()