gpt4 book ai didi

Laravel 通过 2 个不同的模型嵌套 foreach

转载 作者:行者123 更新时间:2023-12-02 10:18:00 27 4
gpt4 key购买 nike

我试过了 this 。问题是,如何从不同模型中获取数据?

Trying to get property of non-object (View: C:\wamp64\www\zainsurgalt\resources\views\choices\index.blade.php)

Controller

$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
]

添加了此 Controller 完整代码

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'));
}
}

View完整代码

<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 nobrwaon​​brwa

最佳答案

1 个解决方案

您正在 Blade 文件中使用选择 ID,但在查询中,您没有加载该 ID所以需要同时加载id和question_number

$choices = Choice::where('user_id',Auth::id())->get(['id','question_number'])->toArray();

此外,当您使用toArray()时,您需要更改

来自 {{ $choice->question_number }}

TO {{ $choice['question_number'] }}

选项编辑链接相同href="choices/{{ $choice['id'] }}/edit"

或者只是从查询中删除 toArray()

2个解决方案

现在,如果您想将选择与主题一起加载,并且您在选择模型中有主题 id

$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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51015571/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com