0

I have a form like this:

<form action="{{ route('popups.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <div id="dynamic_field">
        <label>Date of showing</label>
        <input type="text" id="date" name="datep" class="form-control datepicker" value="" autofocus>
        
        <label for="title" class="control-label">Title</label>
        <input type="text" id="title" name="title" class="form-control" value="" autofocus>

        <label for="link" class="control-label">Link</label>
        <input type="text" id="link" name="linkp[]" class="form-control" value="" autofocus>

        <label for="bio" class="control-label">Text</label>
        <textarea class="form-control" name="bio[]" rows="3"></textarea>

        <label for="filep" class="control-label">Image</label>
        <input type="file" class="form-control-file" id="filep" name="filep[]">

        <button class="btn btn-success" type="submit">Submit</button>

        <a id="add" class="btn btn-info" style="color:white">Add new form</a>
    </div>                                      
</form>

As you can see, I have added a link with id="add" for adding additional form fields dynamically with javascript.

So it means a user can add several images, texts and links. And that's why I used filep[], bio[] and linkp[], as the name of those form fields (in order to save them as an array).

Then at the Controller, I added this:

  public function store(Request $request)
    {
        try{
            $data = $request->validate([
                'datep' => 'nullable',
                'title' => 'nullable',
                'linkp' => 'nullable',
                'bio' => 'nullable',
                'filep' => 'nullable',
            ]);
            
            $newPop = Popup::create([
                'datep' => $data['datep'],
                'title' => $data['title']
            ]);
            
            $files = $request->file('filep');

            if($request->hasFile('filep'))
            {
                foreach ($files as $file) {
                    $newImageName = time() . '-' . $request->name . '.' . $request->filep->extension();
                    $request->filep->move(public_path('popups'), $newImageName);
                    Popup::where('id',$newPop->id)->update(['image_path'=>",".$newImageName]);
                }
            }

        }catch (\Exception $e) {
            dd($e);
        }
        
        return redirect()->back();
    }

So for each image, I tried moving it into public/popups folder and then it should update the existing record of table with id of $newPop->id.

But it shows me this error:

Call to a member function extension() on array

Which is referring to this line:

$newImageName = time() . '-' . $request->name . '.' . $request->filep->extension();

So what's going wrong here? How can I upload multiple images with using array?

5
  • $file not $filep $newImageName = time() . '-' . $request->name . '.' . $file->extension(); Commented Nov 7, 2021 at 8:15
  • @ZrelliMajdi I just changed it to $newImageName = time() . '-' . $request->name . '.' . $request->file->extension(); but again shows me the error! Commented Nov 7, 2021 at 8:20
  • 1
    $file->extension() not $request->file . $file is an item file from your array Commented Nov 7, 2021 at 8:22
  • @ZrelliMajdi Can you write that as answer please Commented Nov 7, 2021 at 8:26
  • What is your database structure? Popup::where('id',$newPop->id)->update(['image_path'=>",".$newImageName]); you'll get only one image by this. Commented Nov 7, 2021 at 8:43

1 Answer 1

1

You can try this -

    if($files=$request->file('filep')){

    foreach ($files as $key=>$file) {
        $extension = $file->getClientOriginalName();
        $fileName = time().'-' .$request->name.'.'.$extension; // I don't know where did you get this $request->name, I didn't find it on your code.
       
        $created = Popup::create([
            'datep' => $request->datep[$key],
            'title' => $request->title[$key],
            'image_path' => $fileName
        ]);

        if($created){

            // $file->move('popups',$fileName); to store in public folder

            //  If you want to keep files in storage folder, you can use following : -

            Storage::disk('public')->put('popups/'.$location,File::get($file));

            // Dont't forget to run 'php artisan storage:link'
            // It will store into your storage folder and you can access it by Storage::url('file_path)
            

        }else{
            // Your error message.
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Use fileName here -Storage::disk('public')->put('popups/'.$fileName,File::get($file));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.