【问题标题】:Passing array from controller to view - Laravel将数组从控制器传递到视图 - Laravel
【发布时间】:2018-06-11 12:59:13
【问题描述】:

我正在尝试制作测验应用程序。我有 600 多个不同类别的问题,如 jp、sn、ei 等。现在我想随机向用户显示不同类别的问题和选项。但是出现错误。有人会帮我做对吗。我试过这样的东西 - 在我的 Controller.php 中

public function index()
{
    $jp = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'JP')->get();
    $sn = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'SN')->get();
    $ei = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'EI')->get();
    $tf = DB::table('e_questions')->take(2)->inRandomOrder()->where('dichotomy', '=', 'TF')->get();
    $equestions = array("$jp","$sn","$ei","$tf");
    //dd($equestions);

  return view('question.english.index', compact('equestions'));
}

在我看来 index.blade.php

             @foreach($equestions as $equestion)
                 <p>{{ $equestion->question }}</p>
                 <p>{{ $equestion->option1 }}</p>
                 <p>{{ $equestion->option2 }}</p>
             @endforeach

【问题讨论】:

  • 我删除了这个。
  • 试试这样 $equestions = array_merge($jp,$sn,$ei,$tf);并请显示您面临的错误。

标签: php arrays laravel multidimensional-array


【解决方案1】:

首先,创建一个这样的数组:

$equestions = [$jp, $sn, $ei, $tf];

然后遍历数据:

@foreach($equestions as $equestionType)
    @foreach($equestionType as $equestion)
        <p>{{ $equestion->question }}</p>
        <p>{{ $equestion->option1 }}</p>
        <p>{{ $equestion->option2 }}</p>
    @endforeach
@endforeach

【讨论】:

  • 非常感谢先生。这是我所期望的。
  • 60k 恭喜。 :)
猜你喜欢
  • 2016-01-21
  • 2017-05-25
  • 2017-01-29
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 1970-01-01
相关资源
最近更新 更多