V
V
Vadim Anghel2018-08-09 18:44:26
Laravel
Vadim Anghel, 2018-08-09 18:44:26

How to upload photos to database in Laravel?

Guys please help me, I don’t know what the problem is, it just doesn’t want to load into the database!
here is the controller upload code

public function store(Request $request)
     {

       $this->validate($request, [
             'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
           ]);

  $article = new Carouseleinfos();
  if ($request->hasFile('images')) {
      $dir = public_path('/images/carousele');
      $extension = strtolower($request->file('images')->getClientOriginalExtension()); // get image extension
      $fileName = str_random() . '.' . $extension; // rename image
      $request->file('images')->move($dir, $fileName);
      $image->images = $fileName;

  }
    $article->text1 = $request->get('text1');
    $article->text2 = $request->get('text2');
    $article->images = str_slug($request->get('images'));
    $article->textbutton = $request->get('textbutton');
    $article->linkbutton = $request->get('linkbutton');

    $article->save();
    return view('crud_4/index');
}

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kairat Ubukulov, 2018-08-15
@jaguar19961

public function store(Request $request) {
       $this->validate($request, [
             'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

  $article = new Carouseleinfos();
  if ($request->hasFile('images')) {
      $dir = public_path('/images/carousele');
      $extension = strtolower($request->file('images')->getClientOriginalExtension()); // get image extension
      $fileName = str_random() . '.' . $extension; // rename image
      $path_image = $dir . $fileName;
      $request->file('images')->move($dir, $fileName);
      $image->images = $fileName;

  }
    $article->text1 = $request->get('text1');
    $article->text2 = $request->get('text2');
    $article->images = str_slug($request->get('images')); // возможно тут сохранить путь к файлу  $path_image
    $article->textbutton = $request->get('textbutton');
    $article->linkbutton = $request->get('linkbutton');

    $article->save();
    return view('crud_4/index');
}

}

Z
ZloAdmin, 2018-08-09
@ZloAdmin

I think you need to change form enctype to multipart/form-data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question