Laravel上传图片,文件名保存到数据库

时间:2020-09-10 21:01:52

标签: laravel-5.8

我正在使用Laravel 5.8和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)
    {
        $article = new Article();
        $article->title = $request->get('title');
        $article->category_id = $request->get('category_id');
        $article->subtitle = $request->get('subtitle');
        $article->image = $request->get('image');
        $article->description = $request->get('description');
        $article->save();
        return back()->with('success', 'Your article has been added successfully. Please wait for the admin to approve.');
    }

查看

{!!形式::打开([[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>

我正在使用此软件包来处理{:https://github.com/cviebrock/eloquent-sluggable

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

例如,当我说图片名称时,我希望图片名称是文章iike的子弹

如果我创建第1条,则会自动创建一条条,即我希望将图像名称保存为数据库中的article-1.jpg(图像扩展名)并将其保存为article-1.jpg。到公用文件夹。

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

            {!! Form::close() !!}

0 个答案:

没有答案