ihorvorotnov's avatar

Upload a file to a model ID subdir

Hi everyone! Im creating Exercises in Nova and use native File and Image fields to handle uploads. However, Id like the files to be stored in a sub directory with model ID:

- public
  - media
    - {id}
      - video.mp4
      - image.jpeg

This works fine when updating an existing model (the ID is already available), but the files are uploaded to base upload path public/media for new Exercises. This means model ID is not available during file upload yet.

Is it possible to achieve this in any other way? How would you do it, e.g.:

  • can you somehow queue uploads (which would be good to speed things up for admins too)? Queued tasks should be able to access model ID, right? Couldnt find any docs about queues in Nova though.
  • listen to created event, get the ID, create dir, move files and update database fields? Sounds doable but quite some extra work.
  • write custom store procedures for media files and handle it there? Will it run after the model is created or still before saving it to the database so no ID is available?
  • just replace the ID with some unique string that can be generated upfront and appended to a path instead? How would you store it so its accessible for subsequent edits? Get it from saved media file path every time or store as a separate field in database?

Im new to both Laravel and Nova and dont know the best practices yet (unless they are covered in one of the videos here). Would appreciate any help/advice on this.

P.S.: I will also generate intermediate sizes for the image later using Intervention package, tried couple things and the best idea Ive came up with is to handle it on saved and deleted model events. Am I doing it wrong here too?

0 likes
4 replies
ronon's avatar

I have never used Nova, but without it, you have access to the database and can get ID from it.

Why should you want to queue Uploads? You should handle them directly after the upload (store them properly) and if you have verified that it's successfull, you can create a job with the file path and do what ever you want with it.

Why don't you fetch the model ID there and store it in the correct directory? Sth. like: UNTESTED Code! Just to give you an idea how to handle it.

// Get the model from the database, if not create it
$model = Model::where('id', $id)->first();

if(is_null($model)){
    $model = Model::create(['your' => 'data']);
}

$dir = storage_path('media/{yourID}');
if(!Storage::exists($dir)){
    mkdir($dir);
}

$path = Storage::putFile('media/{YourID}',  $request->file('file'));

You can also set the model ID in your form

<input type="text" value="{{ $model->id }}" name="model_id">
$model_id = $request->input('model_id');

Since you don't provide any more details, thats the best I can suggest. For more help, provide more details.

ihorvorotnov's avatar

@ronon thanks for your reply but youre suggesting the Laravel-way which wont work - I need a Nova-way of doing stuff. As you said, you have never used Nova and the problem is it does things in its own way and limits you in many cases. Its a Vue.js SPA that of course uses the regular Laravel model under the hood but gives you another layer of abstraction on top of it.

ihorvorotnov's avatar

@ronon Thanks, but I've studied all available docs before asking the question here and I am already utilizing customized paths (and disks) and metadata and everything works fine when updating existing resources. But when I'm creating a new resource, its ID is unavailable yet so none of these works. Thus, the question is mostly about:

  • how to overcome this "there's no ID at this moment yet" for new entries
  • and what's the best practice (if any).

Asked Taylor on Twitter and he confirmed my initial thoughts - just handle it on appropriate model events. I ended up uploading the files initially to public/media/{HASH}, then listening for saved event and renaming the {HASH} directory to model's ID, which is already available (record is stored in the DB). Intermediate sizes are generated at the same time. Then, on deleted event I just purge the dir with contents.

Please or to participate in this conversation.