Загрузить изображение в БД по Laravel 5.8 - PullRequest
0 голосов
/ 28 сентября 2019

Я пытаюсь загрузить изображение из моих папок в БД с помощью PHPMyAdmin, используя laravel

внутри ProductController.php, метод хранения:

 public function store(Request $request)
     {
      Product::create($request->all());
      $formInput=$request->except('ImgSource');

   //        validation
         $this->validate($request,[
             'Name'=>'required',
             'Price'=>'required',
             'type'=>'required',
             'ImgSource'=>'image|mimes:png,jpg,jpeg|max:10000',
         ]);
   //        image upload
         $image=$request->file('ImgSource');
         if($image){
             $imageName=$image->getClientOriginalName();
             $image->move('images',$imageName);

             $formInput['ImgSource']=$imageName;

         }

         Product::create($formInput);
         return redirect()->route('product.index');
     }

и в файле web.php * 1006.*

Route :: resource ('product', 'ProductsController');и я использую форму в create.blade.php

        <div class="form-group">
            {{ Form::label('ImgSource', 'Image') }}
            {{ Form::file('ImgSource',array('class' => 'form-control')) }}
        </div>

Он хранит в таблицах продукты как C: \ xampp \ xampp1 \ tmp \ php46F9.tmp, что мне нужно сделать, чтобы добавить правильные изображения?

Ответы [ 2 ]

0 голосов
/ 28 сентября 2019

Попробуйте это

public function store(Request $request)
     {
        $input = $request->all();
        if ($request->hasFile('ImgSource')) {
                        $destinationPath = public_path().'/images/';
                        $file = $request->ImgSource;
                        $fileName = time() . '.'.$file->clientExtension();
                        $file->move($destinationPath, $fileName);
                        $input['image'] = $fileName;
                    }

         Product::create($input);
         return redirect()->route('product.index');
     }
0 голосов
/ 28 сентября 2019

Попробуйте этот код

if ($request->hasFile('Images')) {
                    $destinationPath = public_path().'/your path/';
                    $file = $request->Images;
                    $fileName = time() . '.'.$file->clientExtension();
                    $file->move($destinationPath, $fileName);
                    $input['ImgSource '] = $fileName;
                }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...