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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?