Css Laravel:如果用户回答了调查,请更改li类
如果用户之前回答了调查,我想为Css Laravel:如果用户回答了调查,请更改li类,css,laravel,class,if-statement,Css,Laravel,Class,If Statement,如果用户之前回答了调查,我想为的每个类更改类 我尝试使用if条件,但没有任何改变 控制器: public function home(Request $request) { $surveys = Survey::get(); foreach ($surveys as $survey) { $user_id = auth()->user()->id; $user_ip = request()->ip(); $hasAnswer = \
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'));
}
视图刀片:
<div class="col-12">
<ul class="list-group">
@forelse ($surveys as $survey)
@if($hasAnswer)
<li class="list-group-item list-group-item-success">
<div>
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}}
<a href="survey/view/{{ $survey->id }}" title="Take Survey" class="secondary-content"><i class="material-icons">send</i></a>
<a href="survey/{{ $survey->id }}" title="Edit Survey" class="secondary-content"><i class="material-icons">edit</i></a>
<a href="survey/answers/{{ $survey->id }}" title="View Survey Answers" class="secondary-content"><i class="material-icons">textsms</i></a>
</div>
</li>
@else
<li class="list-group-item">
<div>
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}}
<a href="survey/view/{{ $survey->id }}" title="Take Survey" class="secondary-content"><i class="material-icons">send</i></a>
<a href="survey/{{ $survey->id }}" title="Edit Survey" class="secondary-content"><i class="material-icons">edit</i></a>
<a href="survey/answers/{{ $survey->id }}" 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>
@forelse($调查作为$调查)
@如果($hasAnswer)
-
{{link_至_路线('detail.survey',$survey->title,['id'=>$survey->id])}
@否则
-
{{link_至_路线('detail.survey',$survey->title,['id'=>$survey->id])}
@恩迪夫
@空的
无需显示任何内容
@endforelse
即使我试着回答一些调查,也没有什么改变,但没有改变班级
public function home(Request $request)
{
$surveys = Survey::get();
$user_id = auth()->user()->id; // you don't need to iterate over and over again for the same value
$user_ip = request()->ip(); // you don't need to iterate over and over again for the same value
foreach ($surveys as $survey) {
$hasAnswer[] = \App\Answer::where(['user_id'=>$user_id , 'survey_id' => $survey->id, 'last_ip'=>$user_ip])->exists();
}
return view('home', compact('surveys','hasAnswer'));
}
刀片文件:
<div class="col-12">
<ul class="list-group">
@forelse ($surveys as $key => $survey)
@if($hasAnswer[$key])
<li class="list-group-item list-group-item-success">
<div>
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}}
<a href="survey/view/{{ $survey->id }}" title="Take Survey" class="secondary-content"><i class="material-icons">send</i></a>
<a href="survey/{{ $survey->id }}" title="Edit Survey" class="secondary-content"><i class="material-icons">edit</i></a>
<a href="survey/answers/{{ $survey->id }}" title="View Survey Answers" class="secondary-content"><i class="material-icons">textsms</i></a>
</div>
</li>
@else
<li class="list-group-item">
<div>
{{ link_to_route('detail.survey', $survey->title, ['id'=>$survey->id])}}
<a href="survey/view/{{ $survey->id }}" title="Take Survey" class="secondary-content"><i class="material-icons">send</i></a>
<a href="survey/{{ $survey->id }}" title="Edit Survey" class="secondary-content"><i class="material-icons">edit</i></a>
<a href="survey/answers/{{ $survey->id }}" 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>
@forelse($key=>$survey的调查)
@如果($hasAnswer[$key])
-
{{link_至_路线('detail.survey',$survey->title,['id'=>$survey->id])}
@否则
-
{{link_至_路线('detail.survey',$survey->title,['id'=>$survey->id])}
@恩迪夫
@空的
无需显示任何内容
@endforelse
注意:代码未经测试。数据存在于同一用户、调查和ip的
Answer
模型中?而且您的$hasAnswer
位于循环内,因此只返回最后一个迭代值。。您需要创建一个数组来存储所有值。