matt-daneshvar / laravel-survey

Create and manage surveys within your Laravel app.
MIT License
250 stars 50 forks source link

Variable inside view #41

Closed espositodaniele closed 1 year ago

espositodaniele commented 1 year ago

Hello,

From the example views in the type partial I can see that there are two variables $value, $disabled where this are passed throw ?

Here is the code snippet:

@component('survey::questions.base', compact('question'))
    @foreach($question->options as $option)
        <div class="custom-control custom-radio">
            <input type="radio"
                   name="{{ $question->key }}"
                   id="{{ $question->key . '-' . Str::slug($option) }}"
                   value="{{ $option }}"
                   class="custom-control-input"
                    {{ ($value ?? old($question->key)) == $option ? 'checked' : '' }}
                    {{ ($disabled ?? false) ? 'disabled' : '' }}

            >
            <label class="custom-control-label"
                   for="{{ $question->key . '-' . Str::slug($option) }}">{{ $option }}
                @if($includeResults ?? false)
                    <span class="text-success">
                        ({{ number_format((new \MattDaneshvar\Survey\Utilities\Summary($question))->similarAnswersRatio($option) * 100, 2) }}%)
                    </span>
                @endif
            </label>
        </div>
    @endforeach
@endcomponent

Thanks

matt-daneshvar commented 1 year ago

Hi @espositodaniele,

The entry point for all question types is questions/single.blade.php, that's where we populate those two values:

<div class="p-4 border-bottom">
    @include(view()->exists("survey::questions.types.{$question->type}") 
        ? "survey::questions.types.{$question->type}" 
        : "survey::questions.types.text",[
            'disabled' => !($eligible ?? true), 
            'value' => $lastEntry ? $lastEntry->answerFor($question) : null,
            'includeResults' => ($lastEntry ?? null) !== null
        ]
    )
</div>

And if you're wondering about $eligible and $lastEntry, they're populated by SurveyComposer.php:

public function compose(View $view)
{
    $view->with([
        'eligible' => $view->survey->isEligible($this->auth->user()),
        'lastEntry' => $view->survey->lastEntry(auth()->user()),
    ]);
}