I am using Laravel 5.4,
I want to import an excel sheet into mysql,
I use Laravel-Excel
package to do this,
I read the docs but I don't know how to insert the excel file's content into mysql after loading it.
docs:http://www.maatwebsite.nl/laravel-excel/docs/import
For example:
There is an article
table in mysql database,it has these columns:
id title content created_at updated_at
And there is an excel file article.xls
,it has the same columns as article
table.
id title content created_at updated_at
1 example1 example1's content 2017-04-02 00:00:00 2017-04-02 00:00:00
2 example2 example2's content 2017-04-02 00:00:00 2017-04-02 00:00:00
I use Laravel-Excel
to load this excel file:
Excel::load('article.xlsx', function($reader) {
//...
});
Question:
How to insert the excel file's content into article
table?
UPDATE:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use App\Article;
class ExcelController extends Controller
{
public function uploadJobs()
{
Excel::load('D:/test/article.xlsx', function($reader) {
foreach ($reader->toArray() as $row) {
Article::firstOrCreate($row);
}
});
}
}
I try this but it doesn't work.why is it?
via zwl1619