Laravel 5.6 图片上传,在数据库中将文章 slug 保存为图片名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49130606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.6 Image Upload, Save article slug as image name in database
提问by Srikanth Gopi
I'm using Laravel 5.6 and Collective HTML.
我正在使用 Laravel 5.6 和 Collective HTML。
I have a table articles and im creating a form to upload a asingle article
我有一个表格文章,我正在创建一个表单来上传一篇文章
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.');
}
View
看法
!! 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() !!}
I'm using thispackage for slugs
我正在使用这个包来处理蛞蝓
When i create an article a slug is automatically formed based on the title. What i want to achieve is to upload a image file(jpg, png, jpeg) and save the image name to the database and the image to public/uploads/articles folder.
当我创建一篇文章时,会根据标题自动形成一个 slug。我想要实现的是上传图像文件(jpg、png、jpeg)并将图像名称保存到数据库并将图像保存到 public/uploads/articles 文件夹。
When i say image name, i want the imagename to be the slug of the article iike for example
例如,当我说图像名称时,我希望图像名称是文章的 slug
If i create Article 1, a slug is automatically created article-1, i want the image name to be article-1.jpg(the image extension) to be saved in the database and save the image article-1.jpg to the public folder.
如果我创建文章1,会自动创建一个slug文章1,我希望图像名称为article-1.jpg(图像扩展名)保存在数据库中并将图像article-1.jpg保存到公共文件夹。
How to rename the file and achieve this functionality.
如何重命名文件并实现此功能。
采纳答案by Adnan Mumtaz
You can use the following code to store the file and slug in your database
您可以使用以下代码将文件和 slug 存储在您的数据库中
/**
* 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.');
}
Above code will convert the title into a slug and save the image of the name as a slug.
上面的代码将标题转换为一个slug,并将名称的图像保存为一个slug。
Hope this helps.
希望这可以帮助。
回答by Joe
Once you have saved the article $article->save()
that object and all of its methods etc become available to you.
保存文章后$article->save()
,该对象及其所有方法等都可供您使用。
The slug package you're using includes a ->slug
property as part of the sluggable
trait, so you can (if you're using the Storage facade) do something like;
您正在使用的 slug 包包含一个->slug
属性作为特征的一部分sluggable
,因此您可以(如果您使用的是 Storage 外观)执行以下操作;
Storage::put($article->slug.'jpg', $request->file('file_field'));
Or if you're not using the flystem implementation you can store the file like so:
或者,如果您不使用 flystem 实现,则可以像这样存储文件:
$request->photo->storeAs('images', $article->slug.'jpg');
$request->photo
being the file field from your form.
$request->photo
是表单中的文件字段。
Obviously you'll want to perform some kind of processing on the file to get it's mimetype (it might not be a jpg) and probably resize it/crop it etc, but this should get you started.
显然,您会希望对文件执行某种处理以获得它的 mimetype(它可能不是 jpg)并可能调整它的大小/裁剪它等,但这应该会让你开始。