3

I am writing a custom command for laravel 5.1 and when i run it just says: [Error exception] Illegal offset type Here is my code:

namespace App\Console\Commands;

use Illuminate\Console\Command;

class InsertDefaultData extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'data:default';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Used for inserting the default data';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $data = array(
                [0] => array(
                        ['name'] => 'Edit',
                        ['slug'] => 'edit',
                        ['view'] => 'admin.edit'
                    ),
                [1] => array(
                        ['name'] => 'Statistics',
                        ['slug'] => 'statistics',
                        ['view'] => 'admin.statistics'
                    ),
                [2] => array(
                        ['name'] => 'Settings',
                        ['slug'] => 'settings',
                        ['view'] => 'admin.settings'
                    ),
                [3] => array(
                        ['name'] => 'Media',
                        ['slug'] => 'media',
                        ['view'] => 'admin.media'
                    )
            );

        foreach ($data as $key => $data) {
            DB::insert('INSERT INTO dashboard_sites (id, name, slug, view) VALUES (NULL, ?, ?, ?)', [$data[$key]['name'], $data[$key]['slug'], $data[$key]['view']]);
        }

        $data = array(
                [0] => array(
                        ['name'] => 'Edit',
                        ['text'] => 'Edit',
                        ['link'] => '/admin/dashboard/edit',
                        ['order'] => 1
                    ),
                [1] => array(
                        ['name'] => 'Statistics',
                        ['text'] => 'Statistics',
                        ['link'] => '/admin/dashboard/statistics',
                        ['order'] => 3
                    ),
                [2] => array(
                        ['name'] => 'Media',
                        ['text'] => 'Media',
                        ['link'] => '/admin/dashboard/media',
                        ['order'] => 2
                    ),
                [3] => array(
                        ['name'] => 'Settings',
                        ['text'] => 'Settings',
                        ['link'] => '/admin/dashboard/settings',
                        ['order'] => 4
                    )
            );

        foreach ($data as $key => $data) {
            DB::insert('INSERT INTO dashboard_menu (id, name, text, link, order) VALUES (NULL, ?, ?, ?, ?)', [$data[$key]['name'], $data[$key]['text'], $data[$key]['link'], $data[$key]['order']]);
        }

    }
}

I wan't it to run through the multidimensional arrays, and then insert the data into my database but it only returns the error, when i run it. Can you help me so i can run it correctly?

2 Answers 2

2

Looks like you're wrapping your array keys in [] which php interprets at arrays. You can't use arrays as offsets/array keys.

Just use:

0 => array( 'name' => 'Edit',...
1 => array(...

And you should be good.

Sign up to request clarification or add additional context in comments.

Comments

0

PHP, unfortunately, does not allow the use of arrays as array keys, as @Zoe Blair said. Literally all you need to do is remove the brackets around the keys!

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.