0

currently i can store file name is database and in public folder of laravel but i want to store file path in database?

My Controller:

$image=$userProfile['image'];
$image1 = base64_decode($image);
$filename = time().'.'.$extension;
Storage::disk('public')->put($filename,$image1);
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $filename]);

How i can store image path in laravel and image name in public folder? i am sending image in json using base64.

Your help will be highly appreciated?

2
  • 1
    You should put path with name like ['profile_pic' => $filePath.$filename] eventhough I don't recommend to store the path in the DB in case it changes later. If it changes you have to modify all records. Commented Apr 17, 2019 at 6:52
  • can you edit in my code? Commented Apr 17, 2019 at 6:54

2 Answers 2

1

Use:

$image = $userProfile['image'];
$image1 = base64_decode($image);
$filename = time().'.'.$extension;
Storage::disk('public')->put($filename,$image1);    
$filePath = Storage::disk('public')->getAdapter()->getPathPrefix();
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $filePath.$filename]);
Sign up to request clarification or add additional context in comments.

1 Comment

From where did you get the variable $extension ?
0

First type this command :

php artisan storage:link

it will create a symlink to public folder.

in your controller, you can write like this :

if ($request->file('image')) {
    $file = $request->file('image')->store('images', 'public');
    $new_product->image = $file;
 }

this code will insert a random string included the path, so you can have something like this on your image column value 'images/shgsqwerfsd.jpg'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.