简体   繁体   中英

How to upload and save multiple images in database with laravel 5.4?

im trying to upload multiple images with laravel 5.4, actually the process to upload images is done, the question is how to store them in database? for example when gonna create a slide show of something that has several images that is uploaded, how to group them so the slide show knows those images are in one group?

can you guys help me?

Make sure you go to config/filesystems and turn 'FILESYSTEM_DRIVER' to 'public'

Model to store image paths:

class UserDocuments extends Model
{
    //
    protected $table = 'user_documents';

    public function store($user_id, $document_link, $file_name)
    {
        $this->user_id = $user_id;
        $this->document_link = $document_link;
        $this->file_name = $file_name;
        $this->save();
    }
}

Code in controller to store files ('document' is file input field name)

if ($request->hasFile('document')) {
            $link = $request->file('document')->store('documents');
        }
    $userDocuments->store(request('user_id'), url($link), $link);

To show if the images are in a group, you would have to create a database column called 'group_id' and sort them accordingly.

We have to store images into one records/row by separting commas.

 class DocumentController extends Controller
 {
    public function store(Request $request)
    {
         $pid = $request->input('pid');
         $input = $request->file('files');

         $picture = array();
         if($request->hasFile('files')) : 
            foreach ($images as $item):
              $extension = $item->getClientOriginalName(); //image extension
              $name = time() . '.' .$extension;           //image name
              $item->move('uploads/documents/', $name);  //move to destination
              $arr[] = $name;      //store image into array                   
            endforeach;
            $picture = implode(",", $arr); //Image separated by comma
         else:
            $picture = '';
         endif;

        DB::table('document')->insert(array('pid' => $pid,'image' => $picture));
        Session::flash('message', 'Multiple Images are uploaded successfully');
        return redirect('/multiple-image');
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM