【发布时间】:2017-07-06 03:27:44
【问题描述】:
我收到这个错误
helpers.php 第 685 行中的错误异常: preg_replace():参数不匹配,pattern是字符串而replacement是数组
我想转换 cmets 实体,但我不知道它会是什么样子。
什么是最好的选择? toJson() 还是 json_encode?p>
这是我的控制器
public function update($id, Request $request)
{
$trip = Trip::find($id);
$trip = Trip::with('comments')->where('id', $id)->first();
$trip->fill($request->input());
if($request->has('comments')){
// foreach($request->comments as $comments){
// $comments = Comment::find($id);
// $comments->fill($request->input());
// $comments->save();
$commentArray=[];
foreach($request->comments as $key=>$commentEntity) {
// dd($commentEntity);
// dd($request->comments);
$comment =Comment::find($id);
$comment->comment=$commentEntity['comment'];
$comment->trip_id=$commentEntity['trip_id'];
$comment->date=date('Y,m,d,G,i,s');
$comment->user_id=$commentEntity['user_id'];
$comment->save();
}
}
if($request->files){
foreach($request->files as $files){
$file = File::find($id);
$file->save();
}
}
$trip->save();
return response()->json($trip);
}
我已经尝试过:
**// $strFromArr = serialize($comment); and
// $comment->toJson(); and this way too
//$json = json_encode($comment);**
【问题讨论】:
标签: php arrays json laravel backend