array_merge():参数#1不是laravel 5.5中的数组

时间:2018-04-25 07:06:54

标签: php arrays laravel

我正面临一些问题,这与laravel中的array_merge有关 我的控制器代码方法:

    public function isConnectedMA()
{
    $user_id = Auth::user()->id;
    if(!empty($user_id)) {
        $isConnectedM = DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get();
        $isConnectedA = DB::table('user_mlc_aweber')->where('user_id', $user_id)->get();
    }
        $MergeArray = array_merge($isConnectedM,$isConnectedA);
    $resultArray = ['status' => 1, 'message' => 'Template uploaded!', 'dataArray' => $MergeArray];
    return Response::json($resultArray,200);

}

我面临的错误是

  

array_merge():参数#1不是数组

我不知道我哪里错了 任何帮助将受到高度赞赏!

5 个答案:

答案 0 :(得分:5)

使用toArray()collections转换为数组

$isConnectedM = DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get()->toArray();
$isConnectedA = DB::table('user_mlc_aweber')->where('user_id', $user_id)->get()->toArray();

答案 1 :(得分:1)

在laravel ->get();中返回集合对象,您应该在此处使用->toArray()

答案 2 :(得分:0)

$ isConnectedM和$ isConnectedA是Collection对象。 top自动收集对象到json转换

left

答案 3 :(得分:0)

@shahzad, array_merge问题的原因我们要传递两个数组,如果我们没有通过则会抛出错误。

 public function isConnectedMA() {
    $user_id = Auth::user()->id;
    // If user id is not passed, return or redirect to login page.
    if (empty($user_id) {
        // TODO : Do logic to retun
        $resultArray = ['status' => 0, 'message' => 'OOPS! seems like user is not logged in.', 'dataArray' => []];
        return Response::json($resultArray, 401);
    }

    $isConnectedM = DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get()->toArray();
    $isConnectedA = DB::table('user_mlc_aweber')->where('user_id', $user_id)->get()->toArray();
    $MergeArray = array_merge($isConnectedM,$isConnectedA); // array_merge((array)$isConnectedM, (array)$isConnectedA);
    $resultArray = ['status' => 1, 'message' => 'Template uploaded!', 'dataArray' => $MergeArray];
    return Response::json($resultArray,200);
}

参考 arry_merge - http://php.net/manual/en/function.array-merge.php

答案 4 :(得分:0)

$isConnectedM$isConnectedA不是arrays。您还可以使用typecastobject转换为array

$isConnectedM = (array) DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get();
$isConnectedA = (array) DB::table('user_mlc_aweber')->where('user_id', $user_id)->get();