jeudi 8 août 2019

Laravel : change the class li if the user answered the survey

I want to change class for each class of

  • if the user answered the survey before

I tried to use if condition but nothing changed

Controller :

public function home(Request $request) 
  {
    $surveys = Survey::get();
    foreach ($surveys as $survey) {
    $user_id = auth()->user()->id;
    $user_ip = request()->ip();
    $hasAnswer = \App\Answer::where(['user_id'=>$user_id , 'survey_id' => $survey->id, 'last_ip'=>$user_ip])->exists();
    }
    return view('home', compact('surveys','hasAnswer'));
  }

view blade :

<div class="col-12">
                        <ul class="list-group">
                            @forelse ($surveys as $survey)
                            @if($hasAnswer)
                            <li class="list-group-item list-group-item-success">
                                    <div>
                                        
                                        <a href="survey/view/" title="Take Survey" class="secondary-content"><i class="material-icons">send</i></a>
                                        <a href="survey/" title="Edit Survey" class="secondary-content"><i class="material-icons">edit</i></a>
                                        <a href="survey/answers/" title="View Survey Answers" class="secondary-content"><i class="material-icons">textsms</i></a>
                                    </div>
                            </li>
                            @else
                            <li class="list-group-item">
                                <div>
                                    
                                    <a href="survey/view/" title="Take Survey" class="secondary-content"><i class="material-icons">send</i></a>
                                    <a href="survey/" title="Edit Survey" class="secondary-content"><i class="material-icons">edit</i></a>
                                    <a href="survey/answers/" title="View Survey Answers" class="secondary-content"><i class="material-icons">textsms</i></a>
                                </div>
                            </li>
                            @endif
                            @empty
                                <p class="flow-text center-align">Nothing to show</p>
                            @endforelse
                        </ul>
                    </div>

Nothing changed even i tried to answer for some surveys but no class changed

Aucun commentaire:

Enregistrer un commentaire