Question

I have a stupid little question.

As I already know a select query in Laravel will always return an array of results, I have this query:

 $id = DB::select(
       'select id from users where username = ?', array(Session::get('theuser')));

by inserting this id into my table

DB::table('characters')->insert(array(
    'id'            => $id,
    'char_name'     => $charname,
    'char_dynasty'  => $dynastyname,                    
    'picture'       => 'Male1.png'                  
));

I will get the error: ksort() expects parameter 1 to be array, string given. How can I get rid of this? Thanks in advance!

Was it helpful?

Solution

At least one of $id, $charname or $dynastyname is an array and should not be. You are using it wrong.

Below is a POC proving this. The output is "Warning: ksort() expects parameter 1 to be array, integer given on line 13" It runs as expected when providing 'id' => 'a'.

function insert(array $values)
{

        if ( ! is_array(reset($values)))
        {
            $values = array($values);
        }

        else
        {
            foreach ($values as $key => $value)
            {
                ksort($value); $values[$key] = $value;
            }
        }

        var_dump($values);
}

insert(array(
    'id'            => array('a'),
    'char_name'     => 2,
    'char_dynasty'  => 3,                    
    'picture'       => 'Male1.png'                  
));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top