Rass21's avatar

array_merge(): Expected parameter 2 to be an array, string given

I am trying to make a temperature converter, and I created a function inside a controller to request which conversion the user selects. However i get this error when I load my view "array_merge(): Expected parameter 2 to be an array, string given". This is my controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TemperatureController extends Controller
{
    public function index(Request $request)
    {
        $selectValue = $request->input('conversions');

        return view('temp', ['selectValue' => $selectValue]);
    }
}

My route:

Route::get('temp', function() {
    return view('temp', 'TemperatureController@index');
});

And my view:

@extends('layout')

<?php 

use Illuminate\Support\Facades\Input;


$fah = 30 * (9.0/5.0) + 32;

?>

@section('content')


<form method="POST" action="temp">
@csrf
    <input type="number" name="celsius" placeholder="Enter Temperature" step="any">
        <select name="conversions">
            <option value="select">Select Conversion</option>
            <option value="fahtocel">Fahrenheit to Celsius</option>
            <option value="celtofah">Celsius to Fahrenheit</option>
        </select>
    <input type="submit" name="submit" value="Convert">
</form>

<?php 



?>

@endsection

I haven't been able to find anything online. Why am I getting this error?

0 likes
5 replies
STEREOH's avatar
STEREOH
Best Answer
Level 18

Well first of all

Route::get('temp', function() {
    return view('temp', 'TemperatureController@index');
});

???

Route::get('temp', 'TemperatureController@index');

This should also solve your problem.

Nakov's avatar

Your controller action is never used by the way, because when you define the route, you use a closure which renders the view. And maybe that's the error that you get there so change your route to this:

Route::get('temp', 'TemperatureController@index');
Rass21's avatar

@STEREOH - Wow .. I must not have gotten enough sleep. Total brainfart. Thank you.

STEREOH's avatar

Plus for your converter to work index()method should only return the view and you should probably make a second method called by a Route::post() to handle the conversion.

1 like
anilbaniya's avatar

You are passing $selectValue as a string from index method to view, It must be an array.

Please or to participate in this conversation.