array_merge(): Argument #1 is not an array in laravel 5.5

10,115

Solution 1

Use toArray() to convert collections to array

$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();

Solution 2

In laravel ->get(); returns the collection object, you should user ->toArray() here.

Share:
10,115

Related videos on Youtube

shahzad1122
Author by

shahzad1122

Updated on June 04, 2022

Comments

  • shahzad1122
    shahzad1122 almost 2 years

    I am facing some issue and this about array_merge in laravel My Controller code method:

        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);
    
    }
    

    the error i am facing is

    array_merge(): Argument #1 is not an array

    i dont know where i am wrong any help will be highly appreciated !

    • The fourth bird
      The fourth bird about 6 years
      You could check var_dump($isConnectedM); and var_dump($isConnectedA);
  • shahzad1122
    shahzad1122 about 6 years
    ok i have solved my problem but here is i am facing one more issue
  • shahzad1122
    shahzad1122 about 6 years
    You can see i put a condition if(!empty) and after that i want to check if this is empty and show some kind of message but i am trying to do this but i am getting unreachable statement.
  • shahzad1122
    shahzad1122 about 6 years
    ok i have solved my problem but here is i am facing one more issue
  • shahzad1122
    shahzad1122 about 6 years
    You can see i put a condition if(!empty) and after that i want to check if this is empty and show some kind of message but i am trying to do this but i am getting unreachable statement.
  • fumi
    fumi about 6 years
    i dont inderstand enough. ` if(!empty) ` collection object .is ... → if($isConnectedM->isEmpty()) or if($isConnectedM->count() > 0)
  • Sohel0415
    Sohel0415 about 6 years
    @shahzad1122 yes, go on, explain your new problem
  • shahzad1122
    shahzad1122 about 6 years
    its not going on if(empty($user_id) condition
  • bharadwaja Gummadi
    bharadwaja Gummadi about 6 years
    @shahzad1122, then print/debug what is the value of $user_id. and check in the database whether is there a record with the given id or not. Before merging the two arrays, check what is the output of these - $isConnectedM = DB::table('user_mlc_mailchimp')->where('user_id', $user_id)->get()