Home > Blockchain >  different name for file in storage and database in Laravel
different name for file in storage and database in Laravel

Time:06-18

i have used Storage Facade to store the file in the public storage, after that store the name in the database, but the name of the field are different? so how can store them in the same name in both public storage and database? , I have use this code below:

 if($request->hasFile('file')) {
          $image = $request->file('file');
          $imageName = $image->getClientOriginalName();
          Storage::disk('public')->put('images', $image);

         Post::create([
             'title' => $request->input('title'),
             'content' => $request->input('content'),
             'user_id' => Auth::id(),
             'file' => $imageName,
         ]);

in the storage stored with this name: ejn0Y8flFGGfjllEussig6EO4LB3onEIGoXlOIJV.png and in the database stored with the original name

CodePudding user response:

use putFileAs.so you can pass custom file name as third param.

Storage::disk('public')->putFileAs('images',$image, $imageName);

Ref: Automatic Streaming

  • Related