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?