learn4u's avatar

Dropzone2 , upload picture using API !

Hello guyz ,

I'am using dropzone for the front end and Laravel for the backend , I have a form and I want to add an uploader for picture too !

my store function has this :

  $task = Task::create([
  'user_id' => auth()->user()->id,
  'category_id' => $request->category_id,
  'sub_id' => $request->sub_id,
  'title' => $request->title
]);

I added new field in my database :

$table->string('image_name');

I created in my public folder new folder : images / which I use it to store my picture ! Could you please help me with your idea how I can do that ?

0 likes
1 reply
realrandyallen's avatar

Try this in your TaskController:

public function store(Request $request)
{
    $path = Storage::putFile('images', $request->file('file'));

    $task = Task::create([
        'user_id' => auth()->user()->id,
        'category_id' => $request->category_id,
        'sub_id' => $request->sub_id,
        'title' => $request->title,
        'image_name' => $path,
    ]);

    return redirect()->route('tasks.index');
}

If you'd rather store just the image name rather than the full path you can just specify the original file name

    'image_name' => $request->file('file')->getClientOriginalName()

https://laravel.com/docs/5.7/filesystem#storing-files

Please or to participate in this conversation.