代码如下:
public function chat($id=1){
Route::view('/chat', 'chat');
$id = View::make('chat.blade', ['reviewer_id' => Reviewer::findOrFail($id)]);
$audiences = DB::table('audience')->get();
$data = [
'id'=>$id,
'audiences'=>$audiences,
'audience_id'=> 2
];
return View::make('chat.blade', ['data'=>$data]);
}
因为代码很简单,所以我转到刀片视图,从数据库中获取数据,获取受众数据,初始化数据数组,将数据返回到chat.blade简单代码,但是在视图中
未定义变量:数据(视图: /Users/userinfo/Sites/chat/resources/views/chat.blade.php)
查看代码:
<div>
@foreach($data->audiences as $info->audience)
{{$info->audience->id}};
@endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>
<?php $__currentLoopData = $data->audiences; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $info->audience): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<?php echo e($info->audience->id); ?>;
<?php endforeach; $__env->popLoop(); $loop =
$__env->getLastLoop();
?>
答案 0 :(得分:0)
在您的视图中将$data->audiences
更改为$data['audiences']
。 $ data是一个数组而不是对象
答案 1 :(得分:0)
如果我这样做,那么我会这样做
public function chat($id = 1){
Route::view('/chat', 'chat'); // I don't know what that is
$reviewer_id = Reviewer::findOrFail($id); // or Reviewer::find($id);
//I Update this little bit : $audiences = DB::table('audience')->get();
$audiences = Audience::all();
$audience_id = 2 ;
return view('chat.blade', compact(['id','audiences','audience_id','reviewer_id']));
}
现在您可以像这样访问刀片文件中以紧凑形式传递的所有变量
<div>
// Depends on what is in the $audiences could be with "$key => $value"
@foreach($audiences as $key)
{{$key->id}};
@endforeach
</div>
<div>
<form action="/" method="post">
<input type="hidden" value={{$reviewer_id}} name="id">
<input type="hidden" value={{$audience_id}} name="id">
<input type="text" name="message">
<input type="submit" value="submit">
</form>
</div>