首页 > 解决方案 > Laravel 由 2 个不同的模型嵌套 foreach

问题描述

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

试图获取非对象的属性(查看: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">№&lt;/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

标签: laraveleloquent

解决方案


1 个解决方案

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

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

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

{{ $choice->question_number }}

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

推荐阅读