GrahamMorbyDev's avatar

Image upload within a form

Hey guys im looking for a guide to upload a image within a form

Its for a blog and want to have it so I can save the file name in the database and the actual image in an uploads folder saved via a form

Does anyone have anything like this!? I have googled but struggled to find anything

0 likes
6 replies
Raimondas's avatar

Form:

{!! Form::open(['url' => 'URL TO POST METHOD',  'files'=>true]) !!}

In the controller

        if(Input::file())
        {
            $imgName = $request->file('image')->getClientOriginalName();
            $request->file('image')->move(public_path('images/', $imgName);
            $product->image = public_path('images/'.$imgName);
            $product->save();
        }
GrahamMorbyDev's avatar

ok so my view looks like this


                    <!--Add the title for the blog post -->
                    <div class="form-group">
                        {!! Form::label('title' , 'Title:') !!}
                        {!! Form::text('title', null , ['class' => 'form-control']) !!}
                    </div>
                    <!-- Add the content for the blog post -->
                    <div class="form-group">
                        {!! Form::label('body' , 'Content:') !!}
                        {!! Form::textarea('body', null , ['class' => 'form-control']) !!}
                    </div>
                    <!-- Add image for blog post -->
                    <div class="form-group">
                        {!! Form::label('image' , 'Feature Image:') !!}
                        {!! Form::file('image', null , ['class' => 'form-control']) !!}
                    </div>
                    <!--Add the time and date -->
                    <div class="form-group">
                        {!! Form::label('published_at' , 'Publish On:') !!}
                        {!! Form::input('date' , 'published_at', date('Y-m-d') , ['class' => 'form-control']) !!}
                    </div>
                    <!-- Submit button to add article -->
                    <div class="form-group">
                        {!! Form::submit('Add Article', ['class' => 'btn btn-primary form-control']) !!}
                    </div>

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

Controller looks like this:

   public function create() 
   {
       Return view('articles.create');
   }
   // Store all articles from the form 
   public function store(CreateArticleRequest $request) 
   {
       Article::create($request->all());

       return redirect('articles');
   }
}

and model is :


use App\Http\Requests\Request;

class CreateArticleRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'title' => 'required|min:3',
            'body' => 'required',
            'published_at' => 'required|date'
        ];
    }
}

keeping it simple here is my migration!

            $table->string('title');
            $table->text('body');
            $table->text('image');
            $table->timestamps();
            $table->timestamp('published_at'); 

Im completely confused by this task.

ctroms's avatar

I second dopzone.js and the lesson bulk file uploads as recommended by @Ruffles. Be sure to check out the documentation at dropzone.js. The lesson was able to cover plenty to get started, including some nice backend design but it is quite flexible. Watch the rest of the Project Flyer Series series too. If memory serves there was some nice refactoring and thumbnail creation examples in later lessons. I think it will clear up some of your questions.

Please or to participate in this conversation.