syed1234
syed1234

Reputation: 835

How to store image path in database - using laravel

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?

Upvotes: 0

Views: 1287

Answers (2)

daffaquraisy
daffaquraisy

Reputation: 244

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'

Upvotes: 0

Andrei Lupuleasa
Andrei Lupuleasa

Reputation: 2762

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]);

Upvotes: 1

Related Questions