Home > Enterprise >  Laravel 9:Call to a member function getClientOriginalExtension() on null in for loop
Laravel 9:Call to a member function getClientOriginalExtension() on null in for loop

Time:10-14

I have a form the containes 5 files which name is "feature_photo_1","feature_photo_2","feature_photo_3","feature_photo_4","feature_photo_5" . So with this im trying to use a for loop to optimize my code . but when i execute the code it gives me the error of

Call to a member function getClientOriginalExtension() on null 

Here is my controller code

# Insert feature_photos
for ($i=1; $i < 5; $i  ) { 
    if($request->featured_photo_.$i)
    {
        $file = $request->file('featured_photo_'.$i);
        $filename = date('YmdHi') . '.' . $file->getClientOriginalExtension();
        $file->move(public_path('upload/featured_project/'), $filename);
        $fphoto->featured_photo_.$i = $filename;
        $fphoto->save();
    }
}

$fphoto->save();

I doubt there is a mistakes i put on $file = $request->file('featured_photo_'.$i); but im dont know how to fix it . Hope can get yours opinion for this problem . Thank you in advance ,Any answer/opinion are appreciated .


code for form

    <form action="{{route('charity.profile.feat-project.new.store')}}" id="add_form" method="POST" enctype="multipart/form-data">
                            @csrf

                            <div >
                                <!-- Project Name -->
                                <div >
                                    <label for="name" >*Featured Project Name:</label>
                                    <input type="text"  name="name" id="name"
                                        value="{{old('name')}}" placeholder="Enter name of the project">
                                    @error('name')
                                        <div >
                                            {{ $message }}
                                        </div>
                                    @enderror
                                </div>

                                <!-- Cover Photo -->
                                <div >
                                    <div >
                                        <label for="cover_photo" >
                                            Cover Photo
                                            <span data-bs-toggle="tooltip" data-bs-placement="bottom" title="Must not exceed 2mb." data-bs-original-title="yes">
                                                <i ></i>
                                            </span>
                                        </label>
                                        <input  name="cover_photo" id="cover_photo" type="file">
                                        @error('cover_photo')
                                            <div >
                                                {{ $message }}
                                            </div>
                                        @enderror
                                    </div>
                                </div>
                            </div>
                            <div >
                                <div >
                                    <div >
                                        <!-- Date of Activity -->
                                        <div >
                                            <label for="started_on" >*Date of Activity:</label>
                                            <input type="date"  name="started_on" id="started_on"
                                                value="{{old('started_on')}}" placeholder="Enter date of the activity">
                                            @error('started_on')
                                            <div >
                                                {{ $message }}
                                            </div>
                                            @enderror
                                        </div>

                                        <!-- No. of Beneficiaries -->
                                        <div >
                                            <label for="total_beneficiaries" >No. of Beneficiaries (Optional):</label>
                                            <input type="number"  name="total_beneficiaries" id="total_beneficiaries"
                                                value="{{old('total_beneficiaries')}}" min="1" max="1000" placeholder="Choose between 1 to 1000...">
                                            @error('total_beneficiaries')
                                            <div >
                                                {{ $message }}
                                            </div>
                                            @enderror
                                        </div>
                                    </div>
                                    <div >
                                        <!-- Sponsors -->
                                        <div >
                                            <label for="sponsors" >Sponsors (Optional):</label>
                                            <input type="text"  name="sponsors" id="sponsors"
                                                value="{{old('sponsors')}}" placeholder="Enter sponsors...">
                                            @error('sponsors')
                                            <div >
                                                {{ $message }}
                                            </div>
                                            @enderror
                                        </div>

                                        <!-- Venue -->
                                        <div >
                                            <label for="venue" >Venue (Optional):</label>
                                            <input type="text"  name="venue" id="venue"
                                                value="{{old('venue')}}" placeholder="Enter venue of the project...">
                                            @error('venue')
                                            <div >
                                                {{ $message }}
                                            </div>
                                            @enderror
                                        </div>
                                    </div>
                                </div>

                                <!-- Preview of Cover Photo -->
                                <div >
                                        <img id="show_cover_photo"  alt="Cover Photo Preview" src="{{ asset('backend/assets/images/placeholder-image.jpg') }}">
                                </div>
                            </div>

                            <div >
                                <!-- Project Objective -->
                                <div >
                                    <label for="objective" >*Objective:</label>
                                    <textarea id="elm1" rows="7" name="objective" placeholder="Enter your project's objective/s..."
                                        maxlength="500">
                                        {{old('objective')}}
                                    </textarea>
                                    @error('name')
                                        <div >
                                            {{ $message }}
                                        </div>
                                    @enderror
                                </div>

                                <!-- Thanksgiving Message -->
                                <div >
                                    <label for="message" >Thanksgiving Message (Optional):</label>
                                    <textarea id="elm2" rows="7" name="message" placeholder="Enter your thanksgiving message to the sponsors..."
                                        maxlength="500">
                                        {{old('message')}}
                                    </textarea>
                                    @error('name')
                                        <div >
                                            {{ $message }}
                                        </div>
                                    @enderror
                                </div>
                            </div>
                            <div >
                                <p >
                                    <em>
                                        Recommended image size is <strong>* 1920 x 380 </strong>
                                        Must not exceed 2MB.

                                    </em>
                                </p>
                            </div>


                            
                            <div  data-masonry='{"percentPosition": true }'>
                                <div >
                                    <!-- Featured Photo 1 -->
                                    <div >
                                        <label for="featured_photo_1" >Featured Photo 1</label>
                                        <input type="file" name="featured_photo_1" id="featured_photo_1" >
                                        @error('featured_photo_1')
                                            <div >
                                                {{ $message }}
                                            </div>
                                        @enderror
                                        <!-- Preview -->
                                        <div>
                                            <img id="showImage1"  alt="Featured Project Photo Preview" src="{{ asset('backend/assets/images/placeholder-image.jpg') }}">
                                        </div>
                                    </div>

                                    <!-- Featured Photo 2 -->
                                    <div >
                                        <label for="featured_photo_1" >Featured Photo 2</label>
                                        <input type="file" name="featured_photo_2" id="featured_photo_2" >
                                        @error('featured_photo_2')
                                            <div >
                                                {{ $message }}
                                            </div>
                                        @enderror
                                        <!-- Preview -->
                                        <div>
                                            <img id="showImage2"  alt="Featured Project Photo Preview" src="{{ asset('backend/assets/images/placeholder-image.jpg') }}">
                                        </div>
                                    </div>

                                    <!-- Featured Photo 3 -->
                                    <div >
                                        <label for="featured_photo_3" >Featured Photo 3</label>
                                        <input type="file" name="featured_photo_3" id="featured_photo_3" >
                                        @error('featured_photo_3')
                                            <div >
                                                {{ $message }}
                                            </div>
                                        @enderror
                                        <!-- Preview -->
                                        <div>
                                            <img id="showImage3"  alt="Featured Project Photo Preview" src="{{ asset('backend/assets/images/placeholder-image.jpg') }}">
                                        </div>
                                    </div>

                                    <!-- Featured Photo 4 -->
                                    <div >
                                        <label for="featured_photo_1" >Featured Photo 4</label>
                                        <input type="file" name="featured_photo_4" id="featured_photo_4" >
                                        @error('featured_photo_4')
                                            <div >
                                                {{ $message }}
                                            </div>
                                        @enderror
                                        <!-- Preview -->
                                        <div>
                                            <img id="showImage4"  alt="Featured Project Photo Preview" src="{{ asset('backend/assets/images/placeholder-image.jpg') }}">
                                        </div>
                                    </div>

                                    <!-- Featured Photo 5 -->
                                    <div >
                                        <label for="featured_photo_5" >Featured Photo 5</label>
                                        <input type="file" name="featured_photo_5" id="featured_photo_5" >
                                        @error('featured_photo_5')
                                            <div >
                                                {{ $message }}
                                            </div>
                                        @enderror
                                        <!-- Preview -->
                                        <div>
                                                 <img id="showImage5"  alt="Featured Project Photo Preview" src="{{ asset('backend/assets/images/placeholder-image.jpg') }}">
                                        </div>
                                    </div>
                                </div>
                            </div>


                            <div >
                                <ul >
                                    <button type="button"  data-bs-target="#bs-add-modal-center" data-bs-toggle="modal">
                                        <i ></i> Submit
                                    </button>
                                    <a  href="{{ url()->previous() }}">Cancel</a>
                                </ul>
                            </div>
                            <div >
                                <p >
                                    <em>
                                        <strong>*Featured projects are subject for approval by Caviom </strong>
                                        before it can be displayed on your Charitable Organization's public profile.
                                        The processing times for approval may usually take from 2 to 3 working days.
                                    </em>
                                </p>
                            </div>

                        </form>

CodePudding user response:

to read a dynamic property you can not just use . here:

$request->featured_photo_.$i

Instead you need to create a variable and use that in the attribute call:

$fileInputName = 'featured_photo_'.$i;
$request->$fileInputName;

your 5th input will not be processed as is :

for ($i=1; $i < 5; $i  ) { 

will only go to 4.

full code:

for ($i=1; $i < 6; $i  ) { 
    $fileInputName = 'featured_photo_'.$i;
    if($request->$fileInputName)
    {
        $file = $request->file($fileInputName);
        $filename = date('YmdHi') . '.' . $file->getClientOriginalExtension();
        $file->move(public_path('upload/featured_project/'), $filename);
        $fphoto->$fileInputName = $filename;
        $fphoto->save();
    }
}
  • Related