Laravel 5.6图像上传,将文章slug保存为数据库中的图像名称

Sri*_*opi 5 php image-uploading laravel

我正在使用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.');
}
Run Code Online (Sandbox Code Playgroud)

视图

!! 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() !!}
Run Code Online (Sandbox Code Playgroud)

我正在使用这个包装进行slu ..

当我创建一篇文章时,会根据标题自动形成一个slug.我想要实现的是上传图像文件(jpg,png,jpeg)并将图像名称保存到数据库,将图像保存到public/uploads/articles文件夹.

当我说图像名称时,我希望图像名称是文章的标题.例如

如果我创建了第1条,则会自动创建第1条slug,我希望图像名称为article-1.jpg(图像扩展名)保存在数据库中并将图像文章-1.jpg保存到公众夹.

如何重命名文件并实现此功能.

Adn*_*taz 5

您可以使用以下代码将文件和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.');
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将标题转换为slug并将名称的图像保存为slug.

希望这可以帮助.