nitz25's avatar

How to save image filename in database?

Hi, somebody please teach me how to save the file name in database? I already know how to store the image in my project by using this method:

public function upload (){

  

        If(Input::hasFile('file')){
            

            $file = Input::file('file');

            $destinationPath = public_path(). '/uploads/';
            $filename = $file->getClientOriginalName();

            $file->move($destinationPath, $filename);

            echo  $filename;
            }
    }

and my problem is I dont know how to save the filename. Any help?

Thanks

0 likes
4 replies
bobbybouwmann's avatar

You can simply save it to a model, for example the user. Just assign the field in the model, also make sure you have a profile column in your users table!

$user = User::create([
    'profile' => $filename,
]);
1 like
zaki's avatar
$path=$img->move($dir,filename);
dd($path);

it will give you the full path of where the image is stored with the name.

1 like
nitz25's avatar
nitz25
OP
Best Answer
Level 2

Got it! Thanks a lot!!!

public function upload (){

        If(Input::hasFile('file')){
            

            $file = Input::file('file');

            $destinationPath = public_path(). '/uploads/';
            $filename = $file->getClientOriginalName();

            $file->move($destinationPath, $filename);

            echo  $filename;
            //echo '<img src="uploads/'. $filename . '"/>';

            $user = ImageTest::create([
                'filename' => $filename,
            ]);
        }
    }

I have model named ImageTest and also a field in the database named filename. Thank you so much! :)

1 like

Please or to participate in this conversation.