Laravel通过2种不同的模型嵌套foreach [英] Laravel nested foreach by 2 different Models
问题描述
我尝试过 此.问题是,如何从不同模型中获取数据?
I tried this. problem is, How to foreach data from different Models?
试图获取非对象的属性(视图:C:\ wamp64 \ www \ zainsurgalt \ resources \ views \ choices \ index.blade.php)
Trying to get property of non-object (View: 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'));
查看
@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
foreach之前的dd($ choices)个结果
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
brwanobr oawnbrnoawbn obrawnor bonb rwanobrwn obrawnobrw aonbanobnaowbonwab onbonawb onrwa onbr awnob rwnobrno rbawnorb noawbnorba nobrwaonbrwa
推荐答案
1个解决方案
您正在刀片文件中使用选择ID,但是在查询中,您没有加载ID 因此需要同时加载id和question_number
You are using the choice id in the blade file but on the query, you are not loading the id So need to load the id and question_number both
$choices = Choice::where('user_id',Auth::id())->get(['id','question_number'])->toArray();
另外,当您使用 toArray()时,您需要更改
Also when you use toArray() you need to change the
发件人 {{ $choice->question_number }}
TO {{ $choice['question_number'] }}
与选择相同,编辑链接href="choices/{{ $choice['id'] }}/edit"
same for the choices edit link href="choices/{{ $choice['id'] }}/edit"
或者只是从查询中删除toArray()
.
Or just remove the toArray()
from the query.
2个解决方案
现在,如果您希望将选择与主题一起加载,并且选择模型中具有主题ID
Now if you want to load the choice along with the topic and you have topic id in the choice model
$duplicates = Question::selectRaw("count('id') as total, topic_id")->with('topic', 'topic.choices')->groupBy('topic_id')->get(); // Id for this table is your question number or any realtion between the Question Model and Choice Model
$choices = Choice::where('user_id',Auth::id())->pluck('question_number','topic_id')->toArray();
return view('choices.index',compact('duplicates','choices'));
@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;">
{{ isset($choices[$duplicate->topic->id]) ? $duplicate->topic->id : '' }}
</td>
<td>
<a class="btn btn-default" href="choices/{{ isset($choices[$duplicate->topic->id]) ? $choices[$duplicate->topic->id] : '' }}/edit">Шинэчлэх</a></td>
</tr>
@endforeach
这篇关于Laravel通过2种不同的模型嵌套foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!