Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I customize the data returned by toArray
    Laravel toArray method Eloquent models notifications customize array PHP


How do I customize the data returned by toArray


To customize the data returned by the `toArray()` method in Laravel, you can override this method in your Eloquent models or notifications.

Here's an example of how you can customize the `toArray()` method in an Eloquent model:

php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * The attributes that should be included in the array.
     *
     * @var array
     */
    protected $visible = ['id', 'title', 'content', 'created_at'];

    /**
     * The attributes that should be excluded from the array.
     *
     * @var array
     */
    protected $hidden = ['updated_at'];

    /**
     * Get the author's name for the post.
     *
     * @return string
     */
    public function getAuthorNameAttribute()
    {
        return $this->user->name;
    }

    /**
     * Convert the model instance to an array.
     *
     * @return array
     */
    public function toArray()
    {
        $array = parent::toArray();
        $array['author_name'] = $this->author_name;
        return $array;
    }
}

In this example, we've:

1. Defined the `$visible` and `$hidden` properties: These properties control which attributes are included or excluded from the array representation of the model.
2. Added a custom `getAuthorNameAttribute()` method: This method returns the author's name for the post, which we'll include in the array representation.
3. Overridden the `toArray()` method: In this method, we first call the parent `toArray()` method to get the default array representation of the model. Then, we add the `author_name` attribute to the array and return the modified array.

When you call `$post->toArray()`, the resulting array will include the `id`, `title`, `content`, `created_at`, and `author_name` keys, while excluding the `updated_at` key.

You can use a similar approach to customize the `toArray()` method in your notification classes:

php
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class NewPostNotification extends Notification implements ShouldQueue
{
    use Queueable;

    protected $post;

    public function __construct($post)
    {
        $this->post = $post;
    }

    public function toArray($notifiable)
    {
        return [
            'id' => $this->post->id,
            'title' => $this->post->title,
            'content' => $this->post->content,
            'author_name' => $this->post->user->name,
            'created_at' => $this->post->created_at->toDateTimeString(),
        ];
    }
}

In this example, we've overridden the `toArray()` method to return a custom array representation of the notification, including the post's `id`, `title`, `content`, `author_name`, and `created_at` values.

By customizing the `toArray()` method, you can control the data that is returned when your models or notifications are converted to an array representation, which can be useful for various purposes, such as API responses, database storage, or other data transformations.

Citations:
[1] https://laravel.com/docs/11.x/broadcasting
[2] https://laracasts.com/discuss/channels/laravel/broadcasting-on-private-channels
[3] https://stackoverflow.com/questions/78249472/laravel-11-private-channel-authorization-failing-after-upgrade-from-laravel-10
[4] https://github.com/laravel/framework/issues/16151
[5] https://www.youtube.com/watch?v=G8xIEejDyDY