@Sinnbeck on the second screenshot, this one:
https://laracasts.com/discuss/channels/javascript/uncaught-typeerror-responseforeach-is-not-a-function-1?page=1&replyId=769528
You can see it is an array of objects... Actually that it should be an array of objects... But in reality you are correct it is an unparsed JSON encoded string!!!
@kiph sorry, there was a long time I didn't use ajax from jQuery, and I oversaw this...
First, remove the additional array wrap from the response:
public function show($id)
{
$data = Aricle::query()->whereHas('categories', function ($query) use ($id){
$query->where('id', $id);
})->get();
return json_encode($data); // <<< CHANGE BACK TO THIS
}
Second, either add the dataType property, so jQuery auto parses the returned JSON string:
function categoryCheckbox(id) {
$('.articleFilters').empty();
$.ajax({
type: 'GET',
url: 'getArticle/' + id,
dataType: 'json',
success: function (response) {
response.forEach(element => {
$('.articleFilters').append(element.id);
});
},
});
}
Reference: https://api.jquery.com/jQuery.ajax/#data-types
Or, parse the JSON string yourself:
function categoryCheckbox(id) {
$('.articleFilters').empty();
$.ajax({
type: 'GET',
url: 'getArticle/' + id,
success: function (response) {
response = JSON.parse(response);
response.forEach(element => {
$('.articleFilters').append(element.id);
});
},
});
}