我正在尝试从Laravel向Vue.js数据发送一个对象数组。此代码仅在App::isLocale('fr') === false
我的意思是没有重音时才有效。
还有另一种方法可以将数组传递给vue.js吗?
控制器
public function dashboard($userToken)
{
$data = [];
$data['projects'] = $this->user->listprojects($userToken);
if (App::isLocale('fr')) {
setlocale(LC_TIME, "fr_FR");
} else {
setlocale(LC_TIME, "en_US");
}
foreach ($data['projects'] as $project) {
$project->date = strftime('%d %B %Y - %H:%M',$project->date) ;
}
return view('dashboard',$data);
}
模型
public function listprojects($userToken){
$projects = DB::table('projects')
->select('project_name as name',
'project_token as token',
->where('project_user_id', $userToken)
->orderBy('project_date_modif', 'desc')
->get();
return $projects;
}
查看
<script>
var app = new Vue({
el: '#app',
data: {
projects: <?php echo htmlspecialchars_decode($projects) ?>
}
});
</script>
答案 0 :(得分:2)
将你的数组转换为像这样的json
return view('dashboard',compact("data"));
然后在Vue.js中将其转换回来。
答案 1 :(得分:0)
问题来自函数strftime
,根据此post有一些错误。所以我改变了控制器:
public function dashboard($userToken)
{
$data = [];
$data['projects'] = $this->user->listprojects($userToken);
if (App::isLocale('fr')) {
setlocale(LC_TIME, "fr_FR");
} else {
setlocale(LC_TIME, "en_US");
}
foreach ($data['projects'] as $project) {
$project->date = utf8_encode(strftime('%d %B %Y - %H:%M',$project->date) );
}
return view('dashboard',compact("data"));
}
像@Babak这样的观点建议:
<script>
var app = new Vue({
el: '#app',
data: {
presentations: <?php echo(json_encode($data['projects'])) ?>
}
});
</script>