Laravel 5.1:命令错误[错误异常]非法偏移类型

时间:2015-11-07 22:40:56

标签: php arrays multidimensional-array foreach laravel-5.1

我正在为laravel 5.1编写一个自定义命令,当我运行它时只是说:     [错误异常]非法偏移类型 这是我的代码:

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']]);
        }

    }
}

我不想通过多维数组运行,然后将数据插入我的数据库,但它只返回错误,当我运行它。 你能帮助我,所以我能正确运行吗?

2 个答案:

答案 0 :(得分:1)

看起来你正在[]中将数组键包装在php中解释数组。您不能将数组用作偏移量/数组键。

只需使用:

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

你应该做得好。

答案 1 :(得分:0)

不幸的是, PHP 不允许允许使用数组作为数组键,正如@Zoe Blair所说。您需要做的就是删除键周围的括号!