再會。拜托,我需要你的幫助。建立一個 laravel 網站,其中 tinymce 在一些文本區域中實現。挑戰在于,如果在編輯器中上傳圖像,它們將存儲為 base64 編碼。這會減慢服務器的速度。我不得不在我的數據庫中將我的數據類型更改為 longtext。如何存儲圖像而不是 base64?以及如何讀取存儲的圖像。我的代碼顯示在我的控制器下方public function create(Request $request){ $categories = BlogCategory::all(); $tags = Tag::all(); if($request->isMethod('post')){ //dd($request); $data = $request->except('name'); $post = new Post; //Title $post->title = $request->title; //Slug $post->publish_date = new Carbon; $slug = $this->createSlug($request->title); $post->slug = $slug; //Category if($request->category_id == "Choose Category") { Session::flash('failure','Please Select A Category To Proceed!'); return redirect()->back(); }else{ $post->category_id = $request->category_id; } //Body $post->body = $request->body; //Author if(isset($request->author)){ $post->author = $request->author; $post->author_slug = Str::slug($post->author,'-'); }else{ $post->author = ""; $post->author_slug = ""; } //User ID $post->user_id = Auth::user()->id; //Keywords if(isset($request->keywords)){ $post->keywords = $request->keywords; }else{ $post->keywords = ""; } //Description if(isset($request->description)){ $post->description = $request->description; }else{ $post->description = ""; }
1 回答

慕萊塢森
TA貢獻1810條經驗 獲得超4個贊
您的標題/描述說明了一些內容,但您的代碼說明了其他內容。
要將文件存儲在數據庫中,列類型必須是 BINARY/BLOB。
要將文件名存儲在數據庫中并將文件存儲在磁盤上,列類型應該是相對于最大文件名長度的 VARCHAR。
除非文件很小,否則不要將文件轉換為 base64,因為它們的大小會增加大約 x3 倍。
要將文件存儲在數據庫中,您可以使用此代碼。在您的控制器內部:
if ($request->hasFile('file'))
{
// If you want to resize the image, use the following method to get temporary file's path.
$request->file('file')->getPathName();
// `get()` retrieves file's content in binary mode
$post->image = request->file('file')->get();
}
- 1 回答
- 0 關注
- 104 瀏覽
添加回答
舉報
0/150
提交
取消