首页 文章

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

提问于
浏览
5

我正在使用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() !!}

我正在使用this包进行slu

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

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

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

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

2 回答

  • 0

    保存文章 $article->save() 后,该对象及其所有方法等对您可用 .

    您正在使用的slug包中包含一个 ->slug 属性作为 sluggable trait的一部分,因此您可以(如果您使用的是Storage facade)执行类似的操作;

    Storage::put($article->slug.'jpg', $request->file('file_field'));
    

    或者,如果您没有使用flystem实现,您可以像这样存储文件:

    $request->photo->storeAs('images', $article->slug.'jpg');
    

    $request->photo 是表单中的文件字段 .

    显然你会想要对文件执行某种处理以获得它的mimetype(它可能不是jpg)并且可能调整它/裁剪它等等,但这应该让你开始 .

  • 4

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

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

    希望这可以帮助 .

相关问题