я пробовал
это . Проблема в том, как получить данные из разных моделей?
Попытка получить свойство необъекта (Представление: C: \ wamp64 \ www \ zainsurgalt \ resources \ views \ choices \ index.blade.php)
Контроллер
$duplicates = Question::selectRaw("count('id') as total, topic_id")->with('topic', 'topic.choices')->groupBy('topic_id')->get();
$choices = Choice::where('user_id',Auth::id())->pluck('question_number')->toArray();
return view('choices.index',compact('duplicates','choices'));
View
@foreach ($duplicates as $duplicate)
<tr>
<td style="text-align: center;">{{ $duplicate->topic->id }}</td>
<td style="text-align: center;">{{ $duplicate->topic->title }}</td>
<td style="text-align: center;">{{ $duplicate->total }}</td>
<td style="text-align: center;">
@foreach ($choices as $choice)
{{ $choice->question_number }}
@endforeach
</td>
<td>
<a class="btn btn-default" href="choices/{{ $choice->id }}/edit">Шинэчлэх</a></td>
</tr>
@endforeach
результат dd ($ выборы) перед foreach
array:34 [▼
0 => 5
1 => 5
2 => 0
3 => 0
4 => 0
...
31 => 0
32 => 0
]
Добавлен полный код этого контроллера
public function index(Choice $choice){
$duplicates = Question::selectRaw("count('id') as total, topic_id")->with('topic', 'topic.choices')->groupBy('topic_id')->get();
$choices = Choice::where('user_id',Auth::id())->pluck('question_number');
$user = Choice::where('user_id','=',Auth::id())->first();
if ($user === null) {
$too = 0;
return redirect()->route('choices.create');
}
else{
$too = 1;
return view('choices.index',compact('too','duplicates','choices'));
}
}
Полный код вида
<table align="center" border="1" cellpadding="1" cellspacing="1" style="height:106px; width:100%">
<thead>
<tr>
<th colspan="5" scope="col">
<h3 style="text-align: center;"><b>Шалгалтын цаг сонголт</b></h3>
<select style="text-align: center;" name="time" class="form-control">
<option value="30:01">30 минут</option>
<option value="40:01">40 минут</option>
<option value="50:01">50 минут</option>
<option value="60:01">60 минут</option>
<option value="70:01">70 минут</option>
<option value="80:01">80 минут</option>
<option value="90:01">90 минут</option>
</select></th>
</tr>
<tr>
<th style="text-align: center;" scope="col">№</th>
<th style="text-align: center;" scope="col">Нэр</th>
<th style="text-align: center;" scope="col">Нийт асуултын тоо</th>
<th style="text-align: center;" scope="col">Асуултын тоо</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach (array_combine($duplicates->toArray(), $choices->toArray()) as $duplicate => $choice){
<tr>
<td style="text-align: center;">{{ $duplicate->topic->id }}</td>
<td style="text-align: center;">{{ $duplicate->topic->title }}</td>
<td style="text-align: center;">{{ $duplicate->total }}</td>
<td style="text-align: center;">{{ $choice->question_number }}</td>
<td><a class="btn btn-default" href="choices/{{ $choice->id }}/edit">Шинэчлэх</a></td>
</tr>
@endforeach
</tbody>
</table>
brwanobr oawnbrnoawbn obrawnor bonb rwanobrwn obrawnobrw aonbanobnaowbonwab onbonawb onrwa onbr awnob rwnobrno rbawnorb noawbnorba nobrwaonbrwa