Search code examples
phplaravellaravel-storage

Laravel Storage put method returns true


In my laravel-application I have a <form> where it is possible to upload multiple files. When I submit the form the multiple files get stored into the database, but the table column attachment, which is supposed to store the path of the file, always displays 1 (true).

if (request()->has('attachment_files')) {
    $files = request()->attachment_files;

    foreach ($files as $file) {

      $filename = $file->getClientOriginalName();
      $extension = $file->getClientOriginalExtension();
      $filesize = $file->getClientSize();

      $path = Storage::disk('local')->put('attachments' . $filename, $extension);
      $data = SingleApplicationFile::create([
         'files_id' => $application->id,
         'single_application_id' => $application->id,
         'attachment' => $path,
         'attachment_name' => $filename,
         'attachment_size' => $filesize,
      ]);

      $attachment_file[] = $data;

      new SingleApplicationFile($attachment_file);
   }
}

As mentioned, the line $path = Storage::disk('local')->put('attachments' . $filename, $extension); always gives me true and in the database column a "1" is stored.

I used this method before for single file upload, and in that case, the mentioned line stores attachments/somefilename.pdf - so what is the issue here


Solution

  • put() returns a boolean value, which is why you are seeing a 1 in your database column and not the string path to your stored file. You might be thinking of putFile() instead.