Laravel 5.6 图片上传,将文章 slug 作为图片名称保存在数据库中
Laravel 5.6 Image Upload, Save article slug as image name in database
我正在使用 Laravel 5.6 和 Collective HTML。
我有一篇 table 篇文章,我正在创建一个表单来上传一篇文章
文章控制器
/**
* 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() !!}
我正在使用 this 鼻涕虫包
当我创建文章时,会根据标题自动形成一个 slug。我想要实现的是上传一个图像文件(jpg、png、jpeg)并将图像名称保存到数据库和图像到 public/uploads/articles 文件夹。
当我说图片名称时,我希望图片名称是文章的别名,例如
如果我创建Article 1,自动创建一个slug article-1,我希望图片名称为article-1.jpg(图片扩展名)保存在数据库中并保存图片article -1.jpg 到 public 文件夹。
如何重命名文件并实现此功能。
保存文章后 $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
是您表单中的文件字段。
显然您需要对文件执行某种处理以获得它的 mime 类型(它可能不是 jpg)并可能调整大小 it/crop 它等,但这应该让您开始。
您可以使用以下代码将文件和 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.');
}
以上代码会将标题转换为 slug,并将名称的图像保存为 slug。
希望对您有所帮助。
我正在使用 Laravel 5.6 和 Collective HTML。
我有一篇 table 篇文章,我正在创建一个表单来上传一篇文章
文章控制器
/**
* 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() !!}
我正在使用 this 鼻涕虫包
当我创建文章时,会根据标题自动形成一个 slug。我想要实现的是上传一个图像文件(jpg、png、jpeg)并将图像名称保存到数据库和图像到 public/uploads/articles 文件夹。
当我说图片名称时,我希望图片名称是文章的别名,例如
如果我创建Article 1,自动创建一个slug article-1,我希望图片名称为article-1.jpg(图片扩展名)保存在数据库中并保存图片article -1.jpg 到 public 文件夹。
如何重命名文件并实现此功能。
保存文章后 $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
是您表单中的文件字段。
显然您需要对文件执行某种处理以获得它的 mime 类型(它可能不是 jpg)并可能调整大小 it/crop 它等,但这应该让您开始。
您可以使用以下代码将文件和 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.');
}
以上代码会将标题转换为 slug,并将名称的图像保存为 slug。
希望对您有所帮助。