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

DInu98's avatar

How do I set a default value for input field?

when try to add default values to my input and I select the image which need to upload into the database input values are hidden when submitting the from there is an some error display like this

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'student_id' cannot be null (SQL: insert into `payments` (`image_path`, `student_id`, `course_id`, `st_id`, `updated_at`, `created_at`) values (images/nSdUrRgdchUSISbrwW1ExkAON6RaAmNyK4tbVgHy.jpg, ?, 1, ?, 2021-09-14 13:40:14, 2021-09-14 13:40:14))

my livewire view look like this

  <form wire:submit.prevent="save" enctype="multipart/form-data">
          
              @foreach ($students as $student)
                <div class="form-group">
                  <label for="">Full Name : </label>
                  <label> <strong>{{ $student->FullName }}</strong></label>
                </div>
                <div class="form-group">
                  <input type="text"  class="form-control"  value="{{ $student->id }}"wire:model.defer="st_id">
                </div>
                <div class="form-group">
                  <label for="">Student ID : </label>
                  <label> <strong>{{ $student->student_id }}</strong></label>
                  <input type="text"  class="form-control"  value="{{ $student->student_id }}"wire:model.defer="student_id" readonly>
                  <span class="text-danger">@error('student_id')  {{ $message }} @enderror</span>
                </div>
              @endforeach
              <hr>

script

<script>
    window.addEventListener('OpenPaymentModal',function(){
      $('.addpayment').find('span').html('');
      $('.addpayment').find('form')[0].reset();
      $('.addpayment').modal('show');
  });

<div class="row">
        @foreach ($courses as $course)
        <div class="col-md-4 col-sm-6">
            <div class="card-body">
                <div class="card mt-3 shadow">
                    <div class="card-header">{{ $course->Name }}</div>
                    <img class="card-img-top center" src="{{ asset('assets/img/about.jpg') }}" alt="Card image cap">
                    <div class="card-body">
                        <hr>
                        <button class="btn btn-warning" wire:click="OpenPaymentModal({{ $course->id}})">Click here to Pay</button>
                    </div>
                </div>
            </div>
        </div>
        @endforeach

controller

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Auth;
use App\Models\course;
use App\Models\Student;
use App\Models\Payment;

use Livewire\WithFileUploads;

class StudPayment extends Component
{
    use WithFileUploads;

    public $photo,$st_id,$student_id;
    public $course;
   

    public function render()
    {
        $courses = Auth::guard('student')->user()->courses()->get();
        $students = Auth::guard('student')->user()->get();
        return view('livewire.stud-payment',[
            'courses'=>$courses,
            'students'=>$students
        ]);
    }

    public function OpenPaymentModal($id)
    {
        $info = Student::find($id);
        $this->course = $info->id;
        $this->dispatchBrowserEvent('OpenPaymentModal',[
            'id'=>$id
        ]);
        
    }

    public function save()
    {
        $this->validate([
            'course'=>'required',
            'photo'=>'image|max:1024',
            
        ]);
        

        $payment = new Payment();

        $payment->image_path = $this->photo->store('images','public');
        $payment->student_id = $this->student_id;
        $payment->course_id = $this->course;
        $payment->st_id = $this->st_id;
        $save = $payment->save();

        if($save){
            $this->dispatchBrowserEvent('ClosepaymenttModal');
        }
    }
}
0 likes
4 replies
Snapey's avatar

You cannot bind to the same public property every time in your loop

wire:model.defer="student_id"
DInu98's avatar

@Snapey hi thank you very much. so how can i submit from with default values?

Snapey's avatar
Snapey
Best Answer
Level 122

if you have an array of input fields then you need an array in your component

Rayray's avatar

Try this in the migrations file( go to folder- database then migrations, look for the migration)

		$table->string('name')->default('No Name');
1 like

Please or to participate in this conversation.