如何使用MongoDB数据库在Laravel中获取数组

时间:2018-05-31 14:45:41

标签: arrays mongodb laravel laravel-5 eloquent

我有以下文件

{
    "_id" : ObjectId("5b1005f8f2468f0fe0007c46"),
    "question" : "my question",
    "answere" : " my answere",
    "options" : [
            {
                    "A" : "dfd"
            },
            {
                    "B" : "fdf"
            },
            {
                    "C" : "fdfdf"
            }
    ],
    "explanation" : "my Explanation",
    "correctans" : "A",
    "updated_at" : ISODate("2018-05-31T14:26:00Z"),
    "created_at" : ISODate("2018-05-31T14:26:00Z")
 }

现在我想打印选项怎么可能。我正在尝试这个。

@foreach($questions as $key => $question)
<tr>                         
 <td>{{$question->question}}</td>  
 <td>{{$question->answere}}</td>
 <td>{{$question->explanation}}</td>
 <td>{{$question->correctans}}</td>
 <td>
    @foreach($question->options as $key => $option )
       {{ $option->A }}
    @endforeach
  </td>
  <td>{{$question->updated_at}}</td>
</tr>
@endforeach

收到此错误

"Trying to get property 'A' of non-object

使用此

获取数据
$questions = Question::all();

请告诉我们如何获取

1 个答案:

答案 0 :(得分:0)

它只是这样工作

 @foreach($question->options as $key=>$option )
     @foreach($option as $key=>$val )
         {{$key}} {{$val}}
     @endforeach
 @endforeach