[英]Laravel 5.6 Image Upload, Save article slug as image name in database
我正在使用Laravel 5.6和Collective HTML。
我有一個表格文章,即時通訊創建了一個表格以上傳單個文章
ArticleController
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$categories = ArticleCategory::pluck('name', 'id');
return view('backend.articles.create', compact('categories'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$article = new Article();
if ($request->hasFile('image')) {
$image = $request->file('image');
$name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads/articles');
$imagePath = destinationPath. "/". $name;
$image->move($destinationPath, $name);
$article->image = $name;
}
$article->title = $request->get('title');
$article->category_id = $request->get('category_id');
// $article->image = str_slug($request->get('image'));
$article->subtitle = $request->get('subtitle');
$article->description = $request->get('description');
$article->save();
return back()->with('success', 'Your article has been added successfully. Please wait for the admin to approve.');
}
視圖
!! Form::open(['route'=>'articles.store']) !!}
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : '' }}">
{!! Form::select('category_id', $categories, null, ['class'=>'form-control', 'placeholder'=>'Choose Category']) !!}
<span class="text-danger">{{ $errors->first('category_id') }}</span>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
{!! Form::text('title', old('title'), ['class'=>'form-control', 'placeholder'=>'Enter Title']) !!}
<span class="text-danger">{{ $errors->first('title') }}</span>
</div>
<div class="form-group {{ $errors->has('subtitle') ? 'has-error' : '' }}">
{!! Form::text('subtitle', old('subtitle'), ['class'=>'form-control', 'placeholder'=>'Upload subtitle']) !!}
<span class="text-danger">{{ $errors->first('subtitle') }}</span>
</div>
<div class="form-group {{ $errors->has('image') ? 'has-error' : '' }}">
{!! Form::file('image', old('image'), ['class'=>'btn-white form-control', 'placeholder'=>'Enter image Url']) !!}
<span class="text-danger">{{ $errors->first('image') }}</span>
</div>
<div class="form-group {{ $errors->has('description') ? 'has-error' : '' }}">
{!! Form::textarea('description', old('description'), ['class'=>'form-control', 'placeholder'=>'Enter Description']) !!}
<span class="text-danger">{{ $errors->first('description') }}</span>
</div>
<div class="form-group">
<button class="btn btn-primary">Submit</button>
</div>
{!! Form::close() !!}
我正在使用這個包來塞
當我創建文章時,會根據標題自動生成一條。 我要實現的是上傳一個圖像文件(jpg,png,jpeg)並將圖像名稱保存到數據庫中,並將圖像保存到public / uploads / articles文件夾中。
當我說圖片名稱時,例如,我希望圖片名稱是文章ikea的子彈
如果我創建第1條,則會自動創建一個條,即第1條,我希望圖像名稱為article-1.jpg(圖像擴展名)保存在數據庫中,並將圖像article-1.jpg公開保存夾。
如何重命名文件並實現此功能。
您可以使用以下代碼將文件和段存儲在數據庫中
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$article = new Article();
if ($request->hasFile('image')) {
$image = $request->file('image');
$name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads/articles');
$imagePath = $destinationPath. "/". $name;
$image->move($destinationPath, $name);
$article->image = $name;
}
$article->title = $request->get('title');
$article->category_id = $request->get('category_id');
// $article->image = str_slug($request->get('image'));
$article->subtitle = $request->get('subtitle');
$article->description = $request->get('description');
$article->save();
return back()->with('success', 'Your article has been added successfully. Please wait for the admin to approve.');
}
上面的代碼會將標題轉換為一個條並將名稱圖像保存為一條。
希望這可以幫助。
保存完文章$article->save()
,該對象及其所有方法等將對您可用。
您正在使用的slug軟件包包含->slug
屬性,作為可sluggable
特性的一部分,因此您可以(如果使用的是Storage Facade)執行類似的操作;
Storage::put($article->slug.'jpg', $request->file('file_field'));
或者,如果您不使用flystem實現,則可以這樣存儲文件:
$request->photo->storeAs('images', $article->slug.'jpg');
$request->photo
是表單中的文件字段。
顯然,您將需要對該文件執行某種處理以使其成為mimetype(它可能不是jpg),並可能對其進行大小調整/裁剪等操作,但這應該可以幫助您入門。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.