M
M
matros972019-03-27 16:34:26
Laravel
matros97, 2019-03-27 16:34:26

How to upload images?

Hello, how is it possible to upload an image, here is the code I wrote

public function store(Request $request)
    {
        $this->validate($request,[
            'title' => 'required',
            'text' => 'required',
            'date' => 'required',
        ]);

        $status = Post::status($request->view);

        if($request->hasFile('img')) {
            $file = $request->file('img');
        
            $input['img'] = $file->getClientOriginalName();
            
            $file->move(public_path().'/upload',$input['img']);
        }

        Post::create([
            'title' => $request->title,
            'date' => $request->date,
            'description' => $request->description,
            'text' => $request->text,
            'view' => $status,
            'category_id' => $request->category_id,
        ]);

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

The image is transferred to the public / upload folder, but when I print the array, it is not there, here is the screen
5c9b7bcfd718b686749946.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Victor Golovko, 2019-03-27
@siptik

Get the image name or path and add the value to the property. You haven't added any information to the object about your file.
Saved to a folder, but how does the object know about it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question