0.安裝目錄:/home/webadmin/html/laravel
1.安裝 laravel 5.7
$composer create-project laravel/laravel --prefer-dist /home/webadmin/html/laravel 5.7
$sudo vi /etc/hosts
1.安裝 laravel 5.7
$composer create-project laravel/laravel --prefer-dist /home/webadmin/html/laravel 5.7
$sudo vi /etc/hosts
$sudo vim /etc/apache2/sites-available/laravel.conf
寫入:
-------------------------------------------------------------------------------------
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster@localhost
DocumentRoot /home/webadmin/html/laravel/public
<Directory /home/webadmin/html/laravel>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
-------------------------------------------------------------------------------------
如果要取消原本80網頁
$sudo a2dissite 000-default.conf
啟用新網頁
$sudo a2ensite laravel.conf
$sudo a2enmod rewrite
$sudo service apache2 restart
<VirtualHost *:80>
ServerName localhost
ServerAdmin webmaster@localhost
DocumentRoot /home/webadmin/html/laravel/public
<Directory /home/webadmin/html/laravel>
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
-------------------------------------------------------------------------------------
如果要取消原本80網頁
$sudo a2dissite 000-default.conf
啟用新網頁
$sudo a2ensite laravel.conf
$sudo a2enmod rewrite
$sudo service apache2 restart
更改特定目錄擁有者為www-data,及777
$cd /home/webadmin/html/laravel/
$sudo chown -R www-data storage/ bootstrap/cache/
$sudo chmod -R 777 storage/ bootstrap/cache/
$sudo chown -R www-data storage/ bootstrap/cache/
$sudo chmod -R 777 storage/ bootstrap/cache/
2.安裝套件與生成配置檔案
$cd /home/webadmin/html/laravel/
$composer require "maatwebsite/excel":"~2.1.0"
編輯 /home/webadmin/html/laravel/config/app.php
'providers' => [
....
Maatwebsite\Excel\ExcelServiceProvider::class,
],
'aliases' => [
....
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
],
生成配置檔案
$php artisan vendor:publish
生成配置檔案
$php artisan vendor:publish
3.建立認證
$cd /home/webadmin/html/laravel/
$php artisan make:auth
$php artisan migrate
4.新增路由:
編輯/home/webadmin/html/laravel/routes/web.php
$sudo pico /home/webadmin/html/laravel/routes/web.php
$php artisan migrate
4.新增路由:
編輯/home/webadmin/html/laravel/routes/web.php
$sudo pico /home/webadmin/html/laravel/routes/web.php
其內容為:
Route::get('AllUserExport','IMorExportsController@AllUserExport')->name('AllUserExport');
Route::get('AllUserDownload/{type}','IMorExportsController@AllUserDownload')->name('AllUserDownload');
Route::post('AllUserImport','IMorExportsController@AllUserImport')->name('AllUserImport');
5.建立控制器:
$php artisan make:controller IMorExportsController --resource
編輯/home/webadmin/html/laravel/app/Http/Controllers/IMorExportsController.php
$sudo pico /home/webadmin/html/laravel/app/Http/Controllers/IMorExportsController.php
其內容為:
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Excel;
use DB;
class IMorExportsController extends Controller
{
public function AllUserExport() {
return view('ImportAndExport');
}
public function AllUserDownload($type) {
$data = User::get()->toArray();
return Excel::create('AllUserDownload',function ($excel) use ($data) {
$excel->sheet('mySheet',function ($sheet) use($data){
$sheet->fromArray($data);
});
})->download($type);
}
public function AllUserImport(Request $request) {
$users = User::get()->toArray();
$request->validate([
'import_file' => 'required'
]);
$path = $request->file('import_file')->getRealPath();
$data = Excel::load($path)->get();
$fail_message = ' ';
if ($data->count()) {
foreach ($data as $key => $value) {
foreach ($users as $userkey => $user) {
if ( $value->email != $user['email']) {
$arr[] = [
'name' => $value->name,
'email' => $value->email,
'password' => bcrypt($value->password),
'created_at' => now(),
'updated_at' => now(),
];
} else {
$fail_message .= $value->email.' ';
}
}
}
if ($fail_message != ' ') {
return back()->with('fail','Duplicate email: '.$fail_message);
}
if(!empty($arr)) {
if ($data->count()) {
foreach ($data as $key => $value) {
foreach ($users as $userkey => $user) {
if ( $value->email != $user['email']) {
$arr[] = [
'name' => $value->name,
'email' => $value->email,
'password' => bcrypt($value->password),
'created_at' => now(),
'updated_at' => now(),
];
} else {
$fail_message .= $value->email.' ';
}
}
}
if ($fail_message != ' ') {
return back()->with('fail','Duplicate email: '.$fail_message);
}
if(!empty($arr)) {
User::insert($arr);
}
}
return back()->with('success','Insert Record successfully.');
}
}
6.建立 Blade 檔案
建立 ImportAndExport.blade.php
$touch /home/webadmin/html/laravel/resources/views/ImportAndExport.blsde.php
編輯/home/webadmin/html/laravel/resources/views/ImportAndExport.blsde.php
$sudo pico /home/webadmin/html/laravel/resources/views/ImportAndExport.blsde.php
其內容:
<html lang="zh-Tw">
<head>
<title> Import and Export Excel xls xlsx and CSV</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="card-text-center">
<div class="card-header">
<h5> Download file </h5>
</div>
<div class="card-body">
<ul class="nav nav-tabs card-header-tabs">
<li class="nav-item">
<a href="{{ route('AllUserDownload','xls') }}" class="nav-link">Download xls</a>
</li>
<li class="nav-item">
<a href="{{ route('AllUserDownload','xlsx') }}" class="nav-link">Download xlsx</a>
</li>
<li class="nav-item">
<a href="{{ route('AllUserDownload','csv') }}" class="nav-link">Download CSV</a>
</li>
</ul>
</div>
</div>
<div class="card">
<div class="card-header">
<h5> Import CSV </h5>
</div>
<div class="card-body">
<form style="border: 1pt solid #a1cbef;margin: 10px;padding: 10px;" action="{{ route('AllUserImport') }}" class="form" method="post" enctype="multipart/form-data">
@csrf
@if ($errors->any())
<div class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert" aria-label="close">x</a>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
@if ( Session::has('success'))
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">x</a>
<p>{{ Session::get('success') }}</p>
</div>
@endif
@if ( Session::has('fail'))
<div class="alert alert-danger">
<a href="#" class="close" data-dismiss="alert" aria-label="close">x</a>
<p>{{ Session::get('fail') }}</p>
</div>
@endif
<input type="file" name="import_file" />
<button class="btn btn-primary">Import CSV</button>
</form>
</div>
</div>
</div>
</body>
</html>
沒有留言:
張貼留言