YuraLons's avatar

Upload File (multiple)

Good day. The question is the following: I have a PictureJobs database which consists of four fields (img_alt, img_title, img_path, works_id). Also in the controller there is a form for adding the main image for using the library. How to correctly implement the form for sending files to the database so that you can pull them out into your project. ** UploadController**

$this->validate($request, [
            'title' => 'required',
            'slug' => 'required',
            'url' => 'max:100',
            'description' => 'required',
            'tools_one' => 'nullable',
            'tools_two' => 'nullable',
            'date_production' => 'required',
        ]);


            $work = new Works([
                'title' => $request->get('title'),
                'slug'  => $request->get('slug'),
                'url' =>  $request->get('url'),
                'description'  => $request->get('description'),
                'tools_one' => $request->get('tools_one'),
                'tools_two' =>  $request->get('tools_two'),
                'date_production' =>  $request->get('date_production'),
            ]);

            if ($request->hasFile('image') && $request->file('image')->isValid()){
                $work->addMediaFromRequest('image')->toMediaCollection('works');
            };
            if ($request->hasFile('images')){
                $images = $work->images;
                $files = $request->file('images');
                foreach ($files as $file) {
                    $extention = $file->getClientOriginalExtension();
                    $fileNameToStore = 'works/' . $work->id . '/images'.sha1_file($file).".".$extention;
                    $path = $file->storeAs('public', $fileNameToStore);
                    $images[] = "/storage/".$fileNameToStore;
                }
                $work->images = $images;
            };
            dd($work);

Form

<form action="{{ route('work.store') }}" method="post" enctype="multipart/form-data">
<div class="flex flex-wrap justify-evenly">
     <label for="Images" class="label-forms">
     <input type="file" name="images">
	 <input type="text" name="img_alt">
	 <input type="text" name="img_title">
</div>
</form>

model Work

public function images() {
   return $this->hasMany(WokrsImages::class);
};
**model Images**
```php 
public function works() {
   return $this->belogsTo(Wokrs::class);
};
0 likes
1 reply
YuraLons's avatar
$images = $request->file('imagesWork');
            $images->store('works');

                foreach ($images as $i) {
                    $filename = $i->getClientOriginalName();
                    $img = WokrsImages::make($i);
                    $img->save($images, $filename);

                    WokrsImages::create([
                        'img-path' => $request->get($filename),
                        'works_id' => $work->id
                    ]);
                };

I changed the code to make it work for sure, but now I get an error

Call to a member function store() on null
Warning: Maximum number of allowable file uploads has been exceeded in Unknown on line 0

Added the following to the view

<input type="file" name="images[]" multiple> 

Please or to participate in this conversation.