Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Aniruddh's avatar

Set value of Avatar/Image field prgramatically

Hello,

I have a requirement where our company fetches employee and client info from a third-party API. Upon entering the internal ID field in the Client or Employee resource, I am using the dependsOn method to set/fill the value of other fields, which works great on almost all field types, except I am not sure how I can do this with Avatar/Image fields.

Is there any way I can achieve this?

0 likes
1 reply
LaryAI's avatar
Level 58

Certainly! If you're using Laravel Nova for your administration panel, setting the value of an Avatar or Image field programmatically can be a bit tricky since these fields are typically meant to handle file uploads. However, you can achieve this by creating a custom mutator in your model that handles the setting of the image path from an external URL.

Here's a step-by-step solution:

  1. In your model, create a custom mutator for the attribute that corresponds to your Avatar/Image field. This mutator will download the image from the given URL and store it in the appropriate directory.
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class Employee extends Model
{
    // ...

    public function setAvatarAttribute($value)
    {
        // Check if the value is a URL
        if (filter_var($value, FILTER_VALIDATE_URL)) {
            // Define the directory where you want to store the avatar
            $directory = 'avatars/';

            // Use basename() to get the filename from the URL
            $filename = basename($value);

            // Use file_get_contents() to fetch the image from the URL
            $fileContents = file_get_contents($value);

            // Store the image in the defined directory
            Storage::put($directory . $filename, $fileContents);

            // Set the path of the image in the database
            $this->attributes['avatar'] = $directory . $filename;
        } else {
            // If the value is not a URL, assume it is a file upload or a path
            $this->attributes['avatar'] = $value;
        }
    }

    // ...
}
  1. When you fetch the employee or client info from the third-party API, set the avatar attribute with the URL of the image.
$employee = Employee::find($id);
$employee->avatar = $imageUrl; // $imageUrl is the URL you get from the API
$employee->save();
  1. Ensure that your Nova resource uses the correct attribute for the Avatar/Image field.
use Laravel\Nova\Fields\Avatar;

// ...

Avatar::make('Avatar', 'avatar') // 'avatar' is the attribute in your model
    // ...
  1. Make sure that your filesystem configuration in config/filesystems.php is set up correctly to handle the storage of your images.
// ...

'disks' => [

    // ...

    'public' => [
        'driver' => 'local',
        'root' => storage_path('app/public'),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    // ...
],

// ...
  1. Finally, create a symbolic link from public/storage to storage/app/public if you haven't already, so that your images are accessible from the web.
php artisan storage:link

Remember to handle any exceptions that may occur during the file download process and to ensure that the directory permissions are set correctly so that your application can write to the storage directory.

This solution assumes that you are allowed to store and serve the images from your own server. If the images must be served directly from the third-party API, you would need to handle this differently, perhaps by overriding the way the Avatar/Image field is displayed in Nova to use the external URL directly.

Please or to participate in this conversation.