Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Randy_Johnson's avatar

Livewire Live Strange Occurance with Image Upload

Just remember everything is in testing. Also remember that this project was to help me learn and understand livewire.

There is a strange occurrence when uploading a student. When uploading from the file directory of desktop/teachers, the image uploads no problem first time and displays. But changing folder to such as desktop/students the first happening is that it doesn't show, but then trying again the image shows, then adding here on out it seems to display the previous image shown.

https://school-tools.online/roster
    {{-- Add Student --}}
    <form autocomplete="off">
        <div class="modal fade" id="addStudent" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" wire:ignore.self>
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Add Student</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <div class="form-group row">
                            <div class="col-lg-3">
                                <label for="" class="col-sm-2 col-form-label text-right">Teacher</label>
                            </div>
                            <div class="col-lg-9">
                                <select wire:model.defer="teacherSelection" class="form-control" id="exampleFormControlSelect1">
                                    <option> -- select an option -- </option>
                                    @foreach($teachers as $teacher)
                                        <option value="{{$teacher}}">{{$teacher->name}}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-lg-3">
                                <label for="" class="col-sm-2 col-form-label text-right">Photo</label>
                            </div>
                            <div class="col-lg-9">
                                <div class="custom-file">
                                    <input type="file" class="custom-file-input" id="custom-file-input" wire:model.defer="studentPhoto">
                                    <label class="custom-file-label" for="customFile">Choose file</label>
                                </div>
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-lg-3">
                                <label for="" class="col-sm-2 col-form-label text-right">Forename</label>
                            </div>
                            <div class="col-lg-9">
                                <input type="text" class="form-control" id="" placeholder="" wire:model.defer="studentName">
                            </div>
                        </div>
                        <div class="form-group row">
                            <div class="col-lg-3">
                                <label for="" class="col-sm-2 col-form-label text-right">Surname</label>
                            </div>
                            <div class="col-lg-9">
                                <input type="text" class="form-control" id="" placeholder="" wire:model.defer="studentSurname">
                            </div>
                        </div>

                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" class="btn btn-primary" data-dismiss="modal" wire:click="addStudent">Save changes</button>
                    </div>
                </div>
            </div>
        </div>
    </form>
public function addStudent()
    {
        
        $teacher = json_decode($this->teacherSelection);

        $this->validate([
            'studentPhoto' => 'nullable|image|max:10000',
        ]);

        $student = new Student();
        $student->name = $this->studentName;
        $student->surname = $this->studentSurname;
        $imageLocation = $this->studentPhoto ?  $this->studentPhoto->store('public/img/students') : 'no-photo-available.png';
        $student->image = $imageLocation;

        $student->save();

        $attendance = Attendance::create([
            'teacher_id' => $teacher->id,
            'student_id' => $student->id,
            'day' => date('d'),
            'month' => date('F'),
            'year' => date('Y'),
        ]);

        DB::table('teacher_students')->insert([
            'teacher_id' => $teacher->id,
            'student_id' => $student->id,
        ]);

        $this->getStudents();

        $this->reset(['photo', 'studentPhoto']);
    }
    public function getStudents()
    {
        $this->studentsTeacher = DB::table('students')
            ->join('teacher_students', 'students.id', '=', 'teacher_students.student_id')
            ->where('teacher_students.teacher_id', $this->teacherId)
            ->select('students.*')
            ->get();
    }
0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

are you sure you are not uploading from the same folder that you are storing them in?

Please or to participate in this conversation.