I
I
igordata12020-08-08 09:18:57
Laravel
igordata1, 2020-08-08 09:18:57

Why can't I see the $file variable?

public function store(Request $request)
    {
//        dd('stop');

        $this->validate($request, [
            'title' => 'required',
            'slug'   =>  'required|unique:products',
            'text'  =>  'required',
            'path'  =>  'nullable|image',

        ]);

        $product = new Product();
        $product->title = $request->input('title');
        $product->slug = $request->input('slug');
        $product->text = $request->input('text');
        $product->keywords = $request->input('keywords');
        $product->description = $request->input('description');
        $product->published = $request->input('published');
        $product->category_id = $request->input('category_id');
//        $product->product_id = $request->input('product_id');
        $product->price = $request->input('price');
        $product->authorized_price = $request->input('authorized_price');
        $product->short_description = $request->input('short_description');
        $product->save();
//                dd('stop');

        $image = new Image();
//                dd('stop');

        $path =public_path().'uploads/product_images';
//        dd('stop');

        $file = $request->file('file');
        dd($file);

//        dd($path);

        foreach ($file as $f) {
            //        dd($path);

            $filename = str_random(20) .'.' . $f->getClientOriginalExtension() ?: 'png';
            //        dd($path);

            $img = ImageInt::make($f);
            //        dd($path);

            $img->resize(500,500)->save($path . $filename);
            //        dd($path);

            Image::create(['title' => $request->title, 'path' => $filename]);
            //        dd($path);

        }
        //        dd($path);

        $image->path = $request->input('path');
        //        dd($path);

        $image->save();




        return redirect('/product/create')->with('info', 'Данные сохранены');
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Helldar, 2020-09-03
@Helldar

The front should be:

  1. submitting a form with the "multipart/form-data" team;
  2. the "name" attribute of the file field must be equal to "file".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question