Laravel - 'array_merge(): Argument #2 is not an array' occurring when i'm not using an array anymore

14,714

Just use the previous solution but remove the superfluous array assignment.

Instead of using:

$result = ['That email belongs to an existing referrer.']; // shorthand array assignment of string to index 0
// ^ unneeded array assignment ^

Just assign the string directly:

$result = 'That email belongs to an existing referrer.';
return view('admin.invalidReferrer', compact('result'));

Then use the imported data normally as you would:

@section('results')

<h4>{{ $result }}</h4>

@stop
Share:
14,714
AndrewMMG
Author by

AndrewMMG

Updated on June 04, 2022

Comments

  • AndrewMMG
    AndrewMMG almost 2 years

    I was using arrays to pass data back to a view from a controller, but now have switched to using just strings, however this error pops up.

    " array_merge(): Argument #2 is not an array "

    I'm not even using an array anymore, and i've done php artisan clear:cache incase there was some cache I didn't know about. I'm new to laravel but all the results I find are dealing with people incorrectly using arrays, whereas i'm just passing a simple string.

    Can somebody please help? Below is my code

    section of code in controller

    else {
                    $result = 'That email belongs to an existing referrer.';
                    return view('admin.invalidReferrer', $result);
    

    section of code in invalidReferrer.blade.php @extends('admin.admin')

    @section('results')
    
    <h4>{{ $result }}</h4>
    
    
    @stop
    

    previous controller solution

    else {
        $result = ['That email belongs to an existing referrer.'];
        return view('admin.invalidReferrer', compact('result'));
    

    previous blade solution

    @section('results')
    
    <h4>{{ $result[0] }}</h4>
    
    @stop
    
  • Kevin
    Kevin over 7 years
    @Unifx the OP maybe doesn't know that [] is an array assignment, but directly using it inside the second argument works fine too
  • AndrewMMG
    AndrewMMG over 7 years
    Thank you for the responses. I tried that before and it worked, but doesn't compact() return the variable(s) as an array which is still less efficient than passing back a string?
  • Kevin
    Kevin over 7 years
    @AndrewMMG but you can't directly pass a single string inside view as you can't delegate a simple string inside after passing a view. its needs to be inside a key pair value. either use compact or use ['result' => 'string here']
  • AndrewMMG
    AndrewMMG over 7 years
    Oh ok, thank you for the insight. In the laravel documentation it had an example where it just used return view('name', $data) so I was operating under the assumption that I could. Is either of those solutions more efficient than the other? eg: is using a key pair value more efficient than using compact?
  • Kevin
    Kevin over 7 years
    @AndrewMMG i don't think it has any difference at all expect that one is a function and one is another plain array assignment. and most likely $data inside the example contains key pair values. you can't directly pass a single lone string, it needs to be an array. just follow the API, it already says there laravel.com/api/5.2/Illuminate/Routing/…, it already says there, second argument needs an array