AndrewMMG AndrewMMG - 3 months ago 49
PHP Question

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

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

Answer

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