I am making Ajax requests to laravel - but for some reason my custom function is not escaping special characters. I can't figure out why. I have used this exact same function in CodeIgniter and it escapes output just fine. All of the data is getting returned fine to the JS file - but it's not escaping anything. Here is the code:
public function store( Request $request, $project_id ) {
//current logged in user.
$user_id = auth()->user()->id;
//get all post inputs
$inputs = $request->all();
//make sure project ID belongs to current user. Stop someone from adding a task to your project that isn't you.
$projectBelongsToUser = Project::find(1)->where('user_id', $user_id)->where('id', $project_id)->get();
//if a project ID and inputs are provided - log them to the database, if not redirect to home with $errors.
if( $project_id && $inputs['description'] && $projectBelongsToUser ) {
$task = New Task;
$task->description = $inputs['description'];
$task->due_date = $inputs['due_date'];
$task->priority = $inputs['priority'];
$task->completed = 0;
$task->order = 0;
$task->user_id = $user_id;
$task->project_id = $project_id;
$task->save();
//get all tasks
$tasks = Task::where('user_id', $user_id)->where('project_id', $project_id)->orderBy('description', 'asc')->get();
//sanitize tasks for safe output
function sanitize_object_h( $array ) {
$array_modified = $array;
foreach( $array_modified as $object ) {
foreach( $object as &$item ) {
$item = htmlentities( $item, ENT_QUOTES );
}
//end foreach
}
//end foreach
return $array_modified;
}
//end sanitize_object_h
$sanitized_tasks = sanitize_object_h( $tasks );
//return the sanitized object.
echo json_encode( sanitize_object_h( $tasks ) );
} else {
echo "failed";
return;
}//end if
}//end store