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

时间:2017-01-31 17:35:42

标签: php laravel-5.3

如何更改控制器以将实体作为字符串返回html内容?

我在我的一个回调值中接收带有html实体的html示例

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

我认为解决方案涉及使用它。

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

这是我的控制器正在做的事情,我不太明白如何让它回归我想要的。

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 个答案:

答案 0 :(得分:0)

花了一些时间后,我发现我可以将返回的数据映射到数组中并使用html_entity_decode()转换html实体。

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);
}