I have a collection which is an eloquent query. There is one column where I want to replace the value with another value. I am using the transform function to do this however it is not working as intended.
Here is my query in the controller
$articles = KnowledgeBaseArticle::getArticlesByDepartment($department)
->get()
->transform(function ($article) {
$article->category_id = KnowledgeBaseCategory::find($article->category_id)->name;
});
and the getArticlesByDepartment
query from the model:
public function scopeGetArticlesByDepartment($query, $department){
return $query->where('department', $department)
->select('title', 'updated_at', 'department', 'id', 'category_id')
->orderBy('title', 'asc');
}
I want to return it so that all the rows with column category_id
is replaced with the category name. You can see I am trying to do this by using $article->category_id
by using find on the KnowledgeBaseCategory model to retrieve this. However this is not working at all and when I die and dump, I get an single column array full of nulls.
When I have died and dumped $article->category_id
& find query inside the transform, it is returning the correct category name, it is just not replacing the category_id
column with the category name.
I have also tried map instead of transform and got the same result.
If it matters, I am later on converting this data into JSON.
Where am I going wrong? Thanks
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire