0

I have an issue with presenting data from an Eloquent model as JSON.

Let's say I have a Post model extending Eloquent. If in my route I simply return Post::find(1) I will have the following JSON :

{
    id: 1,
    title: "My cool blog post",
    text: "This is very interesting",
    type_id: 2
}

If I have an array of post types somewhere

$types = ["Type1", "Type2", "Type3", "Type4"];

How would I go adding to my json the string containing the type, transforming the string in the process (adding some exclamation marks for example) ? Expected (example):

{
    id: 1,
    title: "My cool blog post",
    text: "This is very interesting",
    type_id: 2,
    type_name: "!Type3!"
}
Lukmo
  • 1,688
  • 5
  • 20
  • 31

1 Answers1

2

You would need to create a Type model and define a one-to-many relationship between the Post model and the Type model

Ayobami Opeyemi
  • 752
  • 7
  • 13