2

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

5 Comments

@Unifx the OP maybe doesn't know that [] is an array assignment, but directly using it inside the second argument works fine too
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?
@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']
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?
@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
-1

Just follow this instructions:

  1. Remove config.php and service.php file from bootstrap.
  2. run php artisan serve
  3. run ctrl+c on terminal after php artisan serve
  4. and run composer update

fixed for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.