Answer the question
In order to leave comments, you need to log in
Is it possible to write data to a variable in a foreach loop?
I’ll give an example code right away (this is a Blade template in Laravel):
@foreach ($todos as $todo)
@if ($todo->public == 1)
@if ($todo->status == 1)
<div class="panel panel-success">
@else
<div class="panel panel-danger">
@endif
<div class="panel-heading">
<h3 class="text-center" style="font-style: italic;"> {{ $todo->title}} </h3>
@foreach ($profiles as $profile)
@if ($profile->id == $todo->user_id)
<p><span class="fw400">Исполнитель:</span> {{ $profile->name }} </p>
@else
<p><span class="fw400">Исполнитель не найден</span></p>
@endif
@endforeach
<span>
<span class="fw400">Начало:</span>
{{$todo->created_at}}
</span>
<span class="fl-right">
<span class="fw400">Время последнего обновления:</span>
{{$todo->updated_at}}
</span>
</div>
<div class="panel-body">
<h5 class="text-center"> {{ $todo->description}} </h5>
<p class="text-justify"> {{ str_limit($todo->content, 250) }}</p>
<a href="/{{$todo->id}}" class="btn btn-primary fl-right">читать полностью</a>
</div>
</div>
@else @break
@endif
@endforeach
@foreach ($profiles as $profile)
@if ($profile->id == $todo->user_id)
<p><span class="fw400">Исполнитель:</span> {{ $profile->name }} </p>
Answer the question
In order to leave comments, you need to log in
@php ($user = $profile->user_id)
or
@php
$user = $profile->user_id
@endphp
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question