laravel 多檔案上傳
============================================================
*資料庫:
news
news_file
*laravel 檔案:
model
migration
controller
route
*postman:
title
content
photo (單一檔案)
file[] (多個檔案)(使用陣列)
============================================================
*資料庫:
news:
id int auto key
title vc
content text
photo vc
migration:
$table->id();
$table->string('title');
$table->text('content');
$table->string('photo');
$table->timestamps();
--------------------------------
news_file:
id int auto key
news_id int
file_name vc 檔案名稱
org_name vc 檔案原始名稱
migration:
$table->id();
$table->unsignedBigInteger('news_id');
$table->string('file_name');
$table->string('org_name');
$table->timestamps();
============================================================
*laravel 檔案:
model
migration
controller
route
--------------------------------
php artisan make:model news -rmc
--------------------------------
route:
use App\Http\Controllers\NewsController;
Route::apiResource('news', NewsController::class);
php artisan route:list
============================================================
*新增
| POST | api/news | news.store | App\Http\Controllers\NewsController@store | api |
POST
http://localhost/animal/public/api/news
============================================================
app\Models\news.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class news extends Model
{
use HasFactory;
//可以被批量寫入的屬性
protected $fillable = [
'title',
'content',
'photo',
];
}
============================================================
app\Models\news_file.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class news_file extends Model
{
use HasFactory;
//可以被批量寫入的屬性
protected $fillable = [
'news_id',
'file_name',
'org_name',
];
}
============================================================
*多檔上傳:
1.把檔案儲存到uploads資料夾
2.把檔案資料儲存到: 資料表 news_file
news_id
file_name
org_name
3.回傳資料
============================================================
app\Http\Controllers\NewsController.php
<?php
namespace App\Http\Controllers;
use App\Models\news;
use App\Models\news_file;
use Illuminate\Http\Request;
class NewsController extends Controller
{
//新增
public function store(Request $request)
{
//-----------------------------------------
//資料驗證
$request->validate([
'photo' => 'required|mimes:jpg,png,gif,jpeg|max:2048', //圖檔
'file[]' => 'mimes:csv,txt,xlx,xls,pdf|max:2048', //檔案
]);
if($request->file()) {
//photo檔案資料
$file = $request->file('photo');
$datax = 'File Name: ' . $file->getClientOriginalName(); // SplitShire-0164-1024x683.jpg
$datax = 'File Extension: ' . $file->getClientOriginalExtension(); // jpg
$datax = 'File Real Path: '.$file->getRealPath(); // C:\wamp64\tmp\php17E8.tmp
$datax = 'File Size: '.$file->getSize(); // 201256 (單位為 byte) (201256 / 1024 = 196 kbyte)
$datax = 'File Mime Type: '.$file->getMimeType(); // image/jpeg
// return $datax;
//儲存圖檔到 uploads 資料夾
$destinationPath = 'uploads';
$fileName = time() . '_' . $file->getClientOriginalName();
$file->move($destinationPath, $fileName);
//新增資料到資料庫
// $data = News::create($request->all());
$data = News::create([
'title' => $request->title,
'content' => $request->content,
'photo' => $fileName,
]);
$data = $data->refresh(); //再查詢一次資料庫
//多檔案上傳-----------------------------------------
if($request->hasFile('file'))
{
$files = $request->file('file');
foreach ($files as $file){
//儲存圖檔到 uploads 資料夾
$destinationPath = 'uploads';
$originalName = $file->getClientOriginalName();
$fileName = time() . '_' . $originalName;
$file->move($destinationPath, $fileName);
//把檔案資料儲存到: 資料表 news_file
$filex = news_file::create([
'news_id' => $data->id,
'file_name' => $fileName,
'org_name' => $originalName,
]);
}
$filex = news_file::where('news_id', $data->id)
->orderBy('id')
->get();
}
return response()->json([
'status' => 'success',
'message' => '新增成功',
'data' => $data,
'file' => $filex,
]);
}
}
}
============================================================
*回傳資料:
{
"status": "success",
"message": "新增成功",
"data": {
"id": 14,
"title": "title001",
"content": "content001",
"photo": "1611499727_SplitShire-0164-1024x683.jpg",
"created_at": "2021-01-24T14:48:48.000000Z",
"updated_at": "2021-01-24T14:48:48.000000Z"
},
"file": [
{
"id": 12,
"news_id": 14,
"file_name": "1611499729_網站規劃.pdf",
"org_name": "網站規劃.pdf",
"created_at": "2021-01-24T14:48:49.000000Z",
"updated_at": "2021-01-24T14:48:49.000000Z"
},
{
"id": 13,
"news_id": 14,
"file_name": "1611499729_test001.txt",
"org_name": "test001.txt",
"created_at": "2021-01-24T14:48:49.000000Z",
"updated_at": "2021-01-24T14:48:49.000000Z"
}
]
}
============================================================
沒有留言:
張貼留言