Am trying to learn how to import a basic excel sheet into laravel 5.1
vname Vservice vphone vmobile
test name test service test number 123232 test mobile 12344
I've made the following function
public function ImportVlist()
{
Excel::load('/import.xlsx', function($reader)
{
$results = $reader->get();
foreach ($results as $key => $value) {
foreach ($value as $key => $value1) {
Vlist::create([
'vname'=>$value1->vname,
'vservice' => $value1->vservice,
'vphone' => $value1->vphone,
'vmobile' => $value1->vmobile
]);
}
}
})->get();
}
I've made a route
Route::get('/vlist/import' , 'VlistsController@ImportVlist');
and in index i've made the following link
<li><a href="{{ action('VlistsController@ImportVlist') }}"> <span>Import Suppliers </span></a></li>
but when i click i receive page not found beside the excel is not imported into mysql db
Action'ed routes should be defined as following:
Route::get('/vlist/import', array('as' => 'vlist.import', 'uses' => 'VlistsController@ImportVlist'));
Also, you can use controller()
helper:
Route::controller('vlist', 'VlistsController', ['getImportVlist' => 'vlist.import']);
Then all methods within that controller with http-method prefixes will be binded to routes like:
public function getImportVlist()
{
Excel::load('/import.xlsx', function($reader)
{
...
}
}
And can be referencet in views via route names:
<li><a href="{{ route('vlist.import') }}"> <span>Import Suppliers </span></a></li>
But personally I preffer pure routes
:
Route::get('/vlist/import', ['uses' => 'VlistsController@ImportVlist', 'as' => 'vlist.import']);
And then you have no need to keep controllers and routes and views in synk:
<li><a href="{{ route('vlist.import') }}"> <span>Import Suppliers </span></a></li>
Pros:
1) routed name is more meaningfull.
2) renaming controller method requires only fix in routes.php
.
3) it's becomes more easier to localise/customize route links/titles/breadcrumbs:
<li><a href="{{ route('vlist.import') }}"> <span>@lang('vlist.import')</span></a></li>
vlist.en.php
<?php
return [
...
'import' => 'Import Suppliers',
...
];
vlist.ru.php
<?php
return [
...
'import' => 'Поставщики',
...
];