> For the complete documentation index, see [llms.txt](https://docs.opensoutheners.com/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opensoutheners.com/home/byte-unit-converter/2.x/use-as-a-laravel-cast.md).

# Use as a Laravel cast

Example of a usage for this library as a Laravel cast on an Eloquent model.

Imagine we have a Laravel based project and one of our models stores bytes so we can convert them freely.

Create the cast using Laravel's command:

```sh
php artisan make:cast ByteUnit
```

Now copy the following in your `app\Casts\ByteUnit.php` file:

```php
<?php

namespace App\Casts;

use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use Illuminate\Database\Eloquent\Model;
use OpenSoutheners\ByteUnitConverter\ByteUnitConverter;

class ByteUnit implements CastsAttributes
{
    /**
     * Cast the given value.
     *
     * @param  array<string, mixed>  $attributes
     */
    public function get(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return (string) ByteUnitConverter::new($value)->nearestUnit();
    }

    /**
     * Prepare the given value for storage.
     *
     * @param  array<string, mixed>  $attributes
     */
    public function set(Model $model, string $key, mixed $value, array $attributes): mixed
    {
        return $value;
    }
}

```

Next and finally you can add this cast class to any of your models that requires this on some of their attributes like so:

```php
<?php

namespace App\Models;

use App\Casts\ByteUnit;
use Illuminate\Database\Eloquent\Model;

class Server extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'disk_available' => ByteUnit::class,
    ];
}
```

And voilá! You now have this cast to your model results on your API or any controller using `toArray` or `toJson` methods.

{% hint style="info" %}
Check more documentation on this at the official Laravel documentation about [Eloquent custom casts](https://laravel.com/docs/10.x/eloquent-mutators#custom-casts).
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.opensoutheners.com/home/byte-unit-converter/2.x/use-as-a-laravel-cast.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
