Laravel change collection attributes name
How would you change the attributes from a collection?
So the return is a Collection of Models with changed attributes, and not a Collection of arrays.
The things is, I would like to make some kinda of translation presenter, so when I get the collection I could dd($collection) and instead of 'name' it would show 'nome', also using response()->json($collection) would show the changed named.
so something like $presenterNames = ['name' => 'nome', 'id' => 'identificador']; and the names that aren't here would call normally.
Collection {#213 ▼
#items: array:2 [▼
0 => Category {#215 ▼
+timestamps: false
#fillable: array:2 [▶]
#connection: "mysql"
#table: "categories"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with:
#withCount:
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
#original: array:3 [▶]
#changes:
#casts:
#dates:
#dateFormat: null
#appends:
#dispatchesEvents:
#observables:
#relations:
#touches:
#hidden:
#visible:
#guarded: array:1 [▶]
#slugOptions: null
}
1 => Category {#219 ▶}
]
}
so basically I would like to change
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
to this
#attributes: array:3 [▼
"identificador" => 1
"nome" => "categoria1"
"slug" => "categoria1"
]
Dinamicaly by using a assoc array.
laravel laravel-5.7 laravel-collection
add a comment |
How would you change the attributes from a collection?
So the return is a Collection of Models with changed attributes, and not a Collection of arrays.
The things is, I would like to make some kinda of translation presenter, so when I get the collection I could dd($collection) and instead of 'name' it would show 'nome', also using response()->json($collection) would show the changed named.
so something like $presenterNames = ['name' => 'nome', 'id' => 'identificador']; and the names that aren't here would call normally.
Collection {#213 ▼
#items: array:2 [▼
0 => Category {#215 ▼
+timestamps: false
#fillable: array:2 [▶]
#connection: "mysql"
#table: "categories"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with:
#withCount:
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
#original: array:3 [▶]
#changes:
#casts:
#dates:
#dateFormat: null
#appends:
#dispatchesEvents:
#observables:
#relations:
#touches:
#hidden:
#visible:
#guarded: array:1 [▶]
#slugOptions: null
}
1 => Category {#219 ▶}
]
}
so basically I would like to change
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
to this
#attributes: array:3 [▼
"identificador" => 1
"nome" => "categoria1"
"slug" => "categoria1"
]
Dinamicaly by using a assoc array.
laravel laravel-5.7 laravel-collection
why you want to change this and why not you change from db?
– Niklesh Raut
Nov 25 '18 at 12:29
because I would like to mantain the database using english conventions, but I would like to be able to translate the key if needed, its really easy to do using fractal, but fractal is only for array or json responses, but I would like to still use the collection if possible if that response isn't array or json.
– Gustavo Fenilli
Nov 25 '18 at 12:34
add a comment |
How would you change the attributes from a collection?
So the return is a Collection of Models with changed attributes, and not a Collection of arrays.
The things is, I would like to make some kinda of translation presenter, so when I get the collection I could dd($collection) and instead of 'name' it would show 'nome', also using response()->json($collection) would show the changed named.
so something like $presenterNames = ['name' => 'nome', 'id' => 'identificador']; and the names that aren't here would call normally.
Collection {#213 ▼
#items: array:2 [▼
0 => Category {#215 ▼
+timestamps: false
#fillable: array:2 [▶]
#connection: "mysql"
#table: "categories"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with:
#withCount:
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
#original: array:3 [▶]
#changes:
#casts:
#dates:
#dateFormat: null
#appends:
#dispatchesEvents:
#observables:
#relations:
#touches:
#hidden:
#visible:
#guarded: array:1 [▶]
#slugOptions: null
}
1 => Category {#219 ▶}
]
}
so basically I would like to change
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
to this
#attributes: array:3 [▼
"identificador" => 1
"nome" => "categoria1"
"slug" => "categoria1"
]
Dinamicaly by using a assoc array.
laravel laravel-5.7 laravel-collection
How would you change the attributes from a collection?
So the return is a Collection of Models with changed attributes, and not a Collection of arrays.
The things is, I would like to make some kinda of translation presenter, so when I get the collection I could dd($collection) and instead of 'name' it would show 'nome', also using response()->json($collection) would show the changed named.
so something like $presenterNames = ['name' => 'nome', 'id' => 'identificador']; and the names that aren't here would call normally.
Collection {#213 ▼
#items: array:2 [▼
0 => Category {#215 ▼
+timestamps: false
#fillable: array:2 [▶]
#connection: "mysql"
#table: "categories"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with:
#withCount:
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
#original: array:3 [▶]
#changes:
#casts:
#dates:
#dateFormat: null
#appends:
#dispatchesEvents:
#observables:
#relations:
#touches:
#hidden:
#visible:
#guarded: array:1 [▶]
#slugOptions: null
}
1 => Category {#219 ▶}
]
}
so basically I would like to change
#attributes: array:3 [▼
"id" => 1
"name" => "categoria1"
"slug" => "categoria1"
]
to this
#attributes: array:3 [▼
"identificador" => 1
"nome" => "categoria1"
"slug" => "categoria1"
]
Dinamicaly by using a assoc array.
laravel laravel-5.7 laravel-collection
laravel laravel-5.7 laravel-collection
edited Nov 25 '18 at 13:01
Gustavo Fenilli
asked Nov 25 '18 at 12:24
Gustavo FenilliGustavo Fenilli
11
11
why you want to change this and why not you change from db?
– Niklesh Raut
Nov 25 '18 at 12:29
because I would like to mantain the database using english conventions, but I would like to be able to translate the key if needed, its really easy to do using fractal, but fractal is only for array or json responses, but I would like to still use the collection if possible if that response isn't array or json.
– Gustavo Fenilli
Nov 25 '18 at 12:34
add a comment |
why you want to change this and why not you change from db?
– Niklesh Raut
Nov 25 '18 at 12:29
because I would like to mantain the database using english conventions, but I would like to be able to translate the key if needed, its really easy to do using fractal, but fractal is only for array or json responses, but I would like to still use the collection if possible if that response isn't array or json.
– Gustavo Fenilli
Nov 25 '18 at 12:34
why you want to change this and why not you change from db?
– Niklesh Raut
Nov 25 '18 at 12:29
why you want to change this and why not you change from db?
– Niklesh Raut
Nov 25 '18 at 12:29
because I would like to mantain the database using english conventions, but I would like to be able to translate the key if needed, its really easy to do using fractal, but fractal is only for array or json responses, but I would like to still use the collection if possible if that response isn't array or json.
– Gustavo Fenilli
Nov 25 '18 at 12:34
because I would like to mantain the database using english conventions, but I would like to be able to translate the key if needed, its really easy to do using fractal, but fractal is only for array or json responses, but I would like to still use the collection if possible if that response isn't array or json.
– Gustavo Fenilli
Nov 25 '18 at 12:34
add a comment |
1 Answer
1
active
oldest
votes
Solution :
In your Model add below code.
protected $appends = ['nome'];
public function getNomeAttribute(){
return $this->attributes['name'];
}
You can access it by querying $modelObj->nome;
Do the same for all needed attributes.
This will also reflect in your JSON response.
Thanks
Reference : https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
wouldn't this be bad for performance? instead of changing directly before making the collection, iterating to change each one sounds a bit heavy performance, and this returns an array and not the collection back, so I can't really use the collection methods or any other after maping it, so I would also not be able to call $something->relationship->something
– Gustavo Fenilli
Nov 25 '18 at 12:41
Can you please specify the input type & return type. You can modify the above code so that it will return array using toArray().
– sadaiMudiNaadhar
Nov 25 '18 at 12:50
basically I just want to change the collection attributes, so return is still going to be a collection, something like AppCategory:all(); returns a collection of categories, I want that collection to have the attributes changed based on what I specified to change, it will work just like the collection you get from all() but just another names for the attributes.
– Gustavo Fenilli
Nov 25 '18 at 12:55
The above code will return the collection. Check return type. Can you elaborate your requirement so that i can suggest something else.
– sadaiMudiNaadhar
Nov 25 '18 at 12:57
Collection {#220 ▼ #items: array:2 [▼ 0 => array:3 [▼ "identificador" => 1 "nome" => "categoria1" "slug" => "categoria1" ] 1 => array:3 [▶] ] } it does return a collection, but inside of it is an array, and not a collection of models, I will change my question to specify that It needs to maintain what kinda of collection it is.
– Gustavo Fenilli
Nov 25 '18 at 13:00
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467398%2flaravel-change-collection-attributes-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Solution :
In your Model add below code.
protected $appends = ['nome'];
public function getNomeAttribute(){
return $this->attributes['name'];
}
You can access it by querying $modelObj->nome;
Do the same for all needed attributes.
This will also reflect in your JSON response.
Thanks
Reference : https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
wouldn't this be bad for performance? instead of changing directly before making the collection, iterating to change each one sounds a bit heavy performance, and this returns an array and not the collection back, so I can't really use the collection methods or any other after maping it, so I would also not be able to call $something->relationship->something
– Gustavo Fenilli
Nov 25 '18 at 12:41
Can you please specify the input type & return type. You can modify the above code so that it will return array using toArray().
– sadaiMudiNaadhar
Nov 25 '18 at 12:50
basically I just want to change the collection attributes, so return is still going to be a collection, something like AppCategory:all(); returns a collection of categories, I want that collection to have the attributes changed based on what I specified to change, it will work just like the collection you get from all() but just another names for the attributes.
– Gustavo Fenilli
Nov 25 '18 at 12:55
The above code will return the collection. Check return type. Can you elaborate your requirement so that i can suggest something else.
– sadaiMudiNaadhar
Nov 25 '18 at 12:57
Collection {#220 ▼ #items: array:2 [▼ 0 => array:3 [▼ "identificador" => 1 "nome" => "categoria1" "slug" => "categoria1" ] 1 => array:3 [▶] ] } it does return a collection, but inside of it is an array, and not a collection of models, I will change my question to specify that It needs to maintain what kinda of collection it is.
– Gustavo Fenilli
Nov 25 '18 at 13:00
|
show 4 more comments
Solution :
In your Model add below code.
protected $appends = ['nome'];
public function getNomeAttribute(){
return $this->attributes['name'];
}
You can access it by querying $modelObj->nome;
Do the same for all needed attributes.
This will also reflect in your JSON response.
Thanks
Reference : https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
wouldn't this be bad for performance? instead of changing directly before making the collection, iterating to change each one sounds a bit heavy performance, and this returns an array and not the collection back, so I can't really use the collection methods or any other after maping it, so I would also not be able to call $something->relationship->something
– Gustavo Fenilli
Nov 25 '18 at 12:41
Can you please specify the input type & return type. You can modify the above code so that it will return array using toArray().
– sadaiMudiNaadhar
Nov 25 '18 at 12:50
basically I just want to change the collection attributes, so return is still going to be a collection, something like AppCategory:all(); returns a collection of categories, I want that collection to have the attributes changed based on what I specified to change, it will work just like the collection you get from all() but just another names for the attributes.
– Gustavo Fenilli
Nov 25 '18 at 12:55
The above code will return the collection. Check return type. Can you elaborate your requirement so that i can suggest something else.
– sadaiMudiNaadhar
Nov 25 '18 at 12:57
Collection {#220 ▼ #items: array:2 [▼ 0 => array:3 [▼ "identificador" => 1 "nome" => "categoria1" "slug" => "categoria1" ] 1 => array:3 [▶] ] } it does return a collection, but inside of it is an array, and not a collection of models, I will change my question to specify that It needs to maintain what kinda of collection it is.
– Gustavo Fenilli
Nov 25 '18 at 13:00
|
show 4 more comments
Solution :
In your Model add below code.
protected $appends = ['nome'];
public function getNomeAttribute(){
return $this->attributes['name'];
}
You can access it by querying $modelObj->nome;
Do the same for all needed attributes.
This will also reflect in your JSON response.
Thanks
Reference : https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
Solution :
In your Model add below code.
protected $appends = ['nome'];
public function getNomeAttribute(){
return $this->attributes['name'];
}
You can access it by querying $modelObj->nome;
Do the same for all needed attributes.
This will also reflect in your JSON response.
Thanks
Reference : https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor
edited Nov 25 '18 at 14:08
answered Nov 25 '18 at 12:34
sadaiMudiNaadharsadaiMudiNaadhar
207110
207110
wouldn't this be bad for performance? instead of changing directly before making the collection, iterating to change each one sounds a bit heavy performance, and this returns an array and not the collection back, so I can't really use the collection methods or any other after maping it, so I would also not be able to call $something->relationship->something
– Gustavo Fenilli
Nov 25 '18 at 12:41
Can you please specify the input type & return type. You can modify the above code so that it will return array using toArray().
– sadaiMudiNaadhar
Nov 25 '18 at 12:50
basically I just want to change the collection attributes, so return is still going to be a collection, something like AppCategory:all(); returns a collection of categories, I want that collection to have the attributes changed based on what I specified to change, it will work just like the collection you get from all() but just another names for the attributes.
– Gustavo Fenilli
Nov 25 '18 at 12:55
The above code will return the collection. Check return type. Can you elaborate your requirement so that i can suggest something else.
– sadaiMudiNaadhar
Nov 25 '18 at 12:57
Collection {#220 ▼ #items: array:2 [▼ 0 => array:3 [▼ "identificador" => 1 "nome" => "categoria1" "slug" => "categoria1" ] 1 => array:3 [▶] ] } it does return a collection, but inside of it is an array, and not a collection of models, I will change my question to specify that It needs to maintain what kinda of collection it is.
– Gustavo Fenilli
Nov 25 '18 at 13:00
|
show 4 more comments
wouldn't this be bad for performance? instead of changing directly before making the collection, iterating to change each one sounds a bit heavy performance, and this returns an array and not the collection back, so I can't really use the collection methods or any other after maping it, so I would also not be able to call $something->relationship->something
– Gustavo Fenilli
Nov 25 '18 at 12:41
Can you please specify the input type & return type. You can modify the above code so that it will return array using toArray().
– sadaiMudiNaadhar
Nov 25 '18 at 12:50
basically I just want to change the collection attributes, so return is still going to be a collection, something like AppCategory:all(); returns a collection of categories, I want that collection to have the attributes changed based on what I specified to change, it will work just like the collection you get from all() but just another names for the attributes.
– Gustavo Fenilli
Nov 25 '18 at 12:55
The above code will return the collection. Check return type. Can you elaborate your requirement so that i can suggest something else.
– sadaiMudiNaadhar
Nov 25 '18 at 12:57
Collection {#220 ▼ #items: array:2 [▼ 0 => array:3 [▼ "identificador" => 1 "nome" => "categoria1" "slug" => "categoria1" ] 1 => array:3 [▶] ] } it does return a collection, but inside of it is an array, and not a collection of models, I will change my question to specify that It needs to maintain what kinda of collection it is.
– Gustavo Fenilli
Nov 25 '18 at 13:00
wouldn't this be bad for performance? instead of changing directly before making the collection, iterating to change each one sounds a bit heavy performance, and this returns an array and not the collection back, so I can't really use the collection methods or any other after maping it, so I would also not be able to call $something->relationship->something
– Gustavo Fenilli
Nov 25 '18 at 12:41
wouldn't this be bad for performance? instead of changing directly before making the collection, iterating to change each one sounds a bit heavy performance, and this returns an array and not the collection back, so I can't really use the collection methods or any other after maping it, so I would also not be able to call $something->relationship->something
– Gustavo Fenilli
Nov 25 '18 at 12:41
Can you please specify the input type & return type. You can modify the above code so that it will return array using toArray().
– sadaiMudiNaadhar
Nov 25 '18 at 12:50
Can you please specify the input type & return type. You can modify the above code so that it will return array using toArray().
– sadaiMudiNaadhar
Nov 25 '18 at 12:50
basically I just want to change the collection attributes, so return is still going to be a collection, something like AppCategory:all(); returns a collection of categories, I want that collection to have the attributes changed based on what I specified to change, it will work just like the collection you get from all() but just another names for the attributes.
– Gustavo Fenilli
Nov 25 '18 at 12:55
basically I just want to change the collection attributes, so return is still going to be a collection, something like AppCategory:all(); returns a collection of categories, I want that collection to have the attributes changed based on what I specified to change, it will work just like the collection you get from all() but just another names for the attributes.
– Gustavo Fenilli
Nov 25 '18 at 12:55
The above code will return the collection. Check return type. Can you elaborate your requirement so that i can suggest something else.
– sadaiMudiNaadhar
Nov 25 '18 at 12:57
The above code will return the collection. Check return type. Can you elaborate your requirement so that i can suggest something else.
– sadaiMudiNaadhar
Nov 25 '18 at 12:57
Collection {#220 ▼ #items: array:2 [▼ 0 => array:3 [▼ "identificador" => 1 "nome" => "categoria1" "slug" => "categoria1" ] 1 => array:3 [▶] ] } it does return a collection, but inside of it is an array, and not a collection of models, I will change my question to specify that It needs to maintain what kinda of collection it is.
– Gustavo Fenilli
Nov 25 '18 at 13:00
Collection {#220 ▼ #items: array:2 [▼ 0 => array:3 [▼ "identificador" => 1 "nome" => "categoria1" "slug" => "categoria1" ] 1 => array:3 [▶] ] } it does return a collection, but inside of it is an array, and not a collection of models, I will change my question to specify that It needs to maintain what kinda of collection it is.
– Gustavo Fenilli
Nov 25 '18 at 13:00
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467398%2flaravel-change-collection-attributes-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why you want to change this and why not you change from db?
– Niklesh Raut
Nov 25 '18 at 12:29
because I would like to mantain the database using english conventions, but I would like to be able to translate the key if needed, its really easy to do using fractal, but fractal is only for array or json responses, but I would like to still use the collection if possible if that response isn't array or json.
– Gustavo Fenilli
Nov 25 '18 at 12:34