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

emilpapelas4@gmail.com's avatar

Survey App Issue

Hello currently i started a new Survey App porject with Laravel and Livewire but i run in some issues. First i want to show the survey data into modal but is showing wrong data, modal will open correctly issues is that the data not showing correctly into it.

my code Blade

<div wire:ignore.self class="modal" id="editsvqModalToggle" aria-hidden="true" aria-labelledby="editsvqModalToggleLabel" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered modal-xl">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="editsvqModalToggleLabel">Add Survey Question</h5>
              <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="px-1 space-y-2">
                    
                    <div class="container p-4 bg-white rounded">
                        <div class="flex justify-between">
                          <div class="flex-col bg-white rounded shadow px-1">
                          <span class="text-gray-700 font-semibold"> {{ $survey->title }}</span>
                          <p class="text-gray-700 font-normal">{{ $survey->description }}</p>
                          </div>
                      </div>
                          <div class="border border-gray-100" style="margin:10px 0px;"></div>
                          <p class="flex items-center justify-center font-bold mb-3">Questions</p>
                          
                              @forelse ($survey->questions as $question)
                    
                              <div x-data="{ opened_tab: null }" class="flex flex-col">
                                <div class="flex flex-col border rounded shadow mb-2">
                                  <div @click="opened_tab = opened_tab == 0 ? null : 0 " class="cursor-pointer px-5 py-3 bg-gray-300 text-gray-700 text-center inline-block shadow hover:-mb-3 rounded-t"> {{ $question->title }} <a href="/question/{{ $question->id }}/edit"  class="bg-yellow-700 text-white rounded px-2 py-1 float-right">Edit</a></div>
                                  <div x-show="opened_tab==0" class="px-4 pb-4 bg-gray-200">
                    
                                    {!! Form::open() !!}
                                    @if($question->question_type === 'text')
                                      {{ Form::text('title')}}
                                    @elseif($question->question_type === 'textarea')
                                    <div class="row">
                                      <div class="input-field col s12">
                                        <label for="textarea1" class="text-gray-700">Provide answer</label>
                                        <textarea id="textarea1" rows="4" class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded border border-gray-300 placeholder-gray-400 focus:outline-none" placeholder="Add your Question"></textarea>
                                      </div>
                                    </div>
                                    @elseif($question->question_type === 'radio')
                                      @foreach($question->option_name as $key=>$value)
                                        <p style="margin:0px; padding:0px;">
                                          <input type="radio" id="{{ $key }}" class="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300" />
                                          <label for="{{ $key }}">{{ $value }}</label>
                                        </p>
                                      @endforeach
                                    @elseif($question->question_type === 'checkbox')
                                      @foreach((array)$question->option_name as $key=>$value)
                                      <p style="margin:0px; padding:0px;">
                                        <input type="checkbox" id="{{ $key }}" class="w-4 h-4 text-blue-600 bg-gray-100 rounded border-gray-300"/>
                                        <label for="{{$key}}">{{ $value }}</label>
                                      </p>
                                      @endforeach
                                    @endif 
                                  {!! Form::close() !!}
                                  </div>
                                </div>
                              @empty
                                <span style="padding:10px;">Nothing to show. Add questions below.</span>
                              @endforelse
                          
                          <h2 class="text-white font-bold flex items-center justify-center">Add Question</h2>
                          <div class="bg-white rounded shadow p-2">
                          <form method="POST" action="{{ $survey->id }}/questions" id="boolean">
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            <div class="row">
                              <div class="input-field col s12">
                                <select class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded focus:outline-none block w-full p-2.5" name="question_type" id="question_type">
                                  <option value="" disabled selected>Choose your option</option>
                                  <option value="text">Text</option>
                                  <option value="textarea">Textarea</option>
                                  <option value="checkbox">Checkbox</option>
                                  <option value="radio">Radio Buttons</option>
                                </select>
                              </div>                
                              <div class="p-1 mb-2">
                                <label for="title" class="text-gray-700 font-semibold">Question</label>
                                <input name="title" id="title" type="text" class="bg-gray-50 border border-gray-300 text-gray-700 text-sm rounded focus:outline-none block w-full p-1" required>
                              </div>  
                              <!-- this part will be chewed by script in init.js -->
                              <span class="form-g"></span>
                    
                              <div class="input-field col s12">
                              <button type="submit" class="px-2 py-1 rounded bg-green-700 text-white">Submit</button>
                              </div>
                            </div>
                            </form>
                          </div>
                      </div>

            </div>
          </div>
        </div>
      </div>
        </div>
      </div>

Livewire Controller

public function editsurveyq(Survey $survey)
    {
      //$this->survey=$survey;
      $survey->load('questions.user');
      $this->dispatchBrowserEvent('show-editsqmodal');
    }

Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Survey extends Model
{
    protected $fillable = ['title', 'description', 'user_id'];
    protected $dates = ['deleted_at'];
    protected $table = 'survey';
  
    public function questions() {
      return $this->hasMany(Question::class);
    }
  
    public function user() {
      return $this->belongsTo(User::class);
    }
    
    public function answers() {
      return $this->hasMany(Answer::class);
    }
}

0 likes
1 reply

Please or to participate in this conversation.