Ad

Laravel 5.6 Image Upload, Save Article Slug As Image Name In Database

- 1 answer

I'm using Laravel 5.6 and 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 this package 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.

When i say image name, i want the imagename to be the slug of the article iike for example

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.

How to rename the file and achieve this functionality.

Ad

Answer

You can use the following code to store the file and slug in your database

 /**
 * 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.

Hope this helps.

Ad
source: stackoverflow.com
Ad