duanshaiduhao2471 2017-01-31 17:35
浏览 129
已采纳

如何防止Laravel在JSON数据中返回htmlEntities?

How can I change my controller to return html content with entities as a string?

I'm receiving html with html entities in one of my callback values for examples

{content: "<p><strong>PART TIME MAINTENANCE</strong>"}

I think the solution involves using this.

HTML::decode('<h1>Hello</h1>');

Here is what my controller is doing, I don't quite understand how to make it return what I want.

class JobController extends Controller{
public function index(){
    $dt = Carbon::now();
    return Response::json(Jobs::where("page_location","=","InternalJobPosting")->where("active","=","1")->where('start_date',"<=",$dt)->where("end_date",">=",$dt)->get(),200);
}
public function show($id){
    return Response::json(Jobs::where("id","=",$id)->first(),200);
}

}

  • 写回答

1条回答 默认 最新

  • douxuanwei1980 2017-01-31 18:39
    关注

    After spending some time I figured out I could map the data returned into an array and transform the html entities using html_entity_decode().

    public function index(){
        $dt = Carbon::now();
        $jobs = Jobs::where("page_location","=","InternalJobPosting")->where("active","=","1")->where('start_date',"<=",$dt)->where("end_date",">=",$dt)->get()
        ->map(function ($job) {
            return [
                'id'=>$job->id,
                'active'=>$job->active,
                'content'=>$job->content,
                'category'=>html_entity_decode($job->category),
                'page_location'=>$job->page_location,
                'page_title'=>$job->page_title,
                'start_date'=>$job->start_date,
                'end_date'=>$job->end_date,
            ];
        });
    
    
        return Response::json($jobs,200);
    }
    public function show($id){
    
        $job = Jobs::where("id","=",$id)->get()
            ->map(function ($job) {
            return [
                'id'=>$job->id,
                'active'=>$job->active,
                'content'=>html_entity_decode($job->content),
                'category'=>$job->category,
                'page_location'=>$job->page_location,
                'page_title'=>$job->page_title,
                'start_date'=>$job->start_date,
                'end_date'=>$job->end_date,
            ];
        });
        return Response::json($job->first(),200);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器