5
votes

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.

2

2 Answers

4
votes

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.

0
votes

Once you have saved the article $article->save() that object and all of its methods etc become available to you.

The slug package you're using includes a ->slug property as part of the sluggable trait, so you can (if you're using the Storage facade) do something like;

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

Or if you're not using the flystem implementation you can store the file like so:

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

$request->photo being the file field from your form.

Obviously you'll want to perform some kind of processing on the file to get it's mimetype (it might not be a jpg) and probably resize it/crop it etc, but this should get you started.