我通过执行以下操作获得了一个集合:
<appender name="fileAppender" class="CustomFileAppender">
<param name="Threshold" value="DEBUG" />
<param name="append" value="true" />
<param name="maxFileSize" value="10MB" />
<param name="maxBackupIndex" value="50" />
<param name="File" value="./log/${current.folder}-wcss-simulator-${current.date}.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss,SSS}\t %-5p\t [%X{ThreadId}]\t %X{MethodName}\t - %m%n" />
</layout>
</appender>
ResponseTemplate.php
public function index(){
$responseTemplates = ResponseTemplate::with('questions.answers')->get();
return $responseTemplates;
}
Question.php
class ResponseTemplate extends Model
{
protected $fillable = ['questions'];
public function questions(){
return $this->belongsToMany('App\Question');
}
}
返回此集合:
class Question extends Model
{
public $timestamps = false;
protected $fillable = [
'title',
'order'
];
public function responseTemplates(){
return $this->belongsToMany('App\ResponseTemplate');
}
}
这正如我所料。
如果我用以下内容替换[
{
id:1,
questions:[
{
id:4,
order:999999,
title:"*How old are you",
pivot:{
response_template_id:1,
question_id:4
},
answers:[
{
id:1,
title:"10+",
question_id:4,
created_at:null,
updated_at:"2017-05-24 09:12:34"
},
{
id:2,
title:"20+",
question_id:4,
created_at:"2017-05-24 08:40:10",
updated_at:"2017-05-24 09:12:34"
}
]
},
{
id:3,
order:999999,
title:"*Where are you from",
pivot:{
response_template_id:1,
question_id:3
},
answers:[
{
id:17,
title:"North",
question_id:3,
created_at:"2017-05-24 09:11:03",
updated_at:"2017-05-24 09:11:11"
},
{
id:18,
title:"South",
question_id:3,
created_at:"2017-05-24 09:11:19",
updated_at:"2017-05-24 09:11:28"
}
]
}
],
created_at:"2017-05-24 15:03:59",
updated_at:"2017-05-24 15:03:59"
},
{
id:10,
questions:[
{
id:8,
order:999999,
title:"*How old are you",
pivot:{
response_template_id:10,
question_id:8
},
answers:[
{
id:36,
title:"30+",
question_id:8,
created_at:"2017-05-24 09:53:17",
updated_at:"2017-05-24 09:55:17"
},
{
id:37,
title:"40+",
question_id:8,
created_at:"2017-05-24 09:55:17",
updated_at:"2017-05-24 09:55:27"
}
]
}
],
created_at:"2017-05-25 11:13:47",
updated_at:"2017-05-25 11:13:47"
}
]
函数:
index()
我明白了:
$responseTemplates = ResponseTemplate::with('questions.answers')->get();
return $responseTemplates[0];
哪个也正确。
但是如果我回来了
{
id: 1,
questions: [
{
id: 4,
order: 999999, // etc
我只是得到一个空字符串。如果我这样做也是如此:
$responseTemplates = ResponseTemplate::with('questions.answers')->get();
return $responseTemplates[0]->questions;
为foreach()提供的参数无效(查看:
我知道我必须做一些非常愚蠢的事,但不能把手指放在上面。我确信我过去已经完成了这一千次,没有任何问题。
任何人都可以帮助我完成我的第二个foreach声明吗?