Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1: Command error [Error exception] Illegal offset type

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?

like image 789
Jonas Hoffmann Avatar asked Dec 03 '25 09:12

Jonas Hoffmann


1 Answers

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.

like image 182
Zoe Blair Avatar answered Dec 04 '25 23:12

Zoe Blair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!