Search code examples
laravel-9laravel-formrequest

How to deal with Image using validated method - Form Request (Laravel 9)


My form has 4 fields name, email, website and image


When only name, email, website fields are passed the following code saves the data perfectly to my DataBase.

public function store(StoreCompanyRequest $request)
    {
         $validated = $request->validated(); //It will return only validated data
         Company::create($validated); 
     
         return response()->json([
            'success' => true,
            'message' => 'Company Created Successfully',
        ]);
    }

but if the user passes an image file. The code above saves the image's temporary path.

This is the output:

enter image description here

Output: $validated

array:4 [  "name" => "infotech"  "email" => "[email protected]"  "logo" => Illuminate\Http\UploadedFile {#310  -test: false  -originalName: "greenscreenman.jpg"  -mimeType: "image/jpeg"  -error: 0  #hashName: null  path: "C:\Users\dummy\AppData\Local\Temp"  filename: "php30CD.tmp"  basename: "php30CD.tmp"  pathname: "C:\Users\dummy\AppData\Local\Temp\php30CD.tmp"  extension: "tmp"  realPath: "C:\Users\dummy\AppData\Local\Temp\php30CD.tmp"  aTime: 2022-03-22 12:52:30  mTime:  2022-03-22 12:52:29  cTime: 2022-03-22 12:52:29  inode: 258750  size: 70458  perms: 0100666  owner: 0  group: 0  type: "file"  writable: true  readable: true  executable: false  file: true  dir: false  link: false  linkTarget: "C:\Users\dummy\AppData\Local\Temp\php30CD.tmp"  }  "website" => "www.infotech.com" ]

I wrote another code that fixes this problem,

public function store(StoreCompanyRequest $request)
    {
         $validated = $request->validated(); //It will return only validated data
        //  Company::create($validated); 

         $company = new Company;
         $company->name = $validated['name'];
         $company->email = $validated['email'];
         $company->website = $validated['website'];

         $logoName = time().'.'.$request->file('logo')->extension(); 
         $logoPath = $request->file('logo')->storeAs('public/files', $logoName);

         $company->logo = $logoName;
         $company->save();

         return response()->json([
            'success' => true,
            'message' => 'Company Created Successfully',
        ]);
    }

But is there any way to save the right location of the image with the very first code block???


Solution

  • is there any way to save the right location of the image with the very first code block

    No, because the first code block does nothing but validate your input before saving it. File uploads require some more work. The docs are a good place to start.

    Here's what I described in my comment.

    If you need some control over the filename:

    public function store(StoreCompanyRequest $request)
    {
        $validated = $request->validated();
    
        $path = $request->file('logo')->storeAs(
            'public/files', 
            time() . '.' . $request->file('logo')->extension()
        );
    
        $validated['logo'] = basename($path);
    
        Company::create($validated); 
    
         return response()->json([
            'success' => true,
            'message' => 'Company Created Successfully',
        ]);
    }
    

    If you don't need control over the filename, you can get Laravel to do a bit more of the work for you:

    public function store(StoreCompanyRequest $request)
    {
        $validated = $request->validated();
    
        $path = $request->file('logo')->store('public/files');
        $validated['logo'] = basename($path);
    
        Company::create($validated); 
    
         return response()->json([
            'success' => true,
            'message' => 'Company Created Successfully',
        ]);
    }