Back

Laravel-5.4 - how to store the image path in database for all users who registers


I am using laravel-5.4 make:auth . Added one extra field in user table -> profile picture in views\auth\register.blade.php. In form, am just choosing an image and uploading it, which inturn stores a temporary path in database like C:\xampp\tmp\php469E.tmp. I want to store the image in public folder and store its image path in database.

I want to do the following: 1. How to store the image path in database for all users who registers.

Have tried defining store function in existing app\Http\Controllers\Auth\RegisterController.php. It doesn't even get into store function. Where and how to write the store function , so that the controller executes it. I am learning laravel for my new project.So any expert advice on how to proceed.

saurabhd replied 2 months ago

Use bellow code for store image in public Directory with resize image and image path store in database field.

   $input           = Input::all();
   $file            = array_get($input, 'image');
   $destinationPath = public_path('images');
   $fileName        = $request->image->getClientOriginalName();
   $imageResize     = Image::make($file->getRealPath())
                   ->resize(50,50,function($c){$c->aspectRatio(); $c->upsize();})->save($destinationPath.'/'.$fileName);  
   $filepath        = $destinationPath.'/'.$path;

   $insert = array('username' => Input::get('username'),
                   'image'    => $filepath,
                   );
    
    DB::table('users')->insert($insert);

Hope, this work for you.

sweetyr replied 2 months ago

Its not even entering into store function after user registers.I have used the existing RegisterController.php which comes in make:auth of laravel [app\Http\Controllers\Auth\RegisterControl‌​ler.php] and defined store function there. when user register in the form[register.blade.php], the action called is-> action="{{ route('register') }}".after the user clicks submit ,it enters into RegisterController to validate user information. My questions is whether we can write functions inside laravel auth\RegisterControl‌​ler.php. In Router.php :

  // Registration Routes...
        $this->get('register', 'Auth\[email protected]')->name('register');
        $this->post('register', 'Auth\[email protected]');

if I add my store function, it doesn't execute. ~~~ $this->post('register', 'Auth\[email protected]');~~~.

So, I tried writing my store function inside \vendor\laravel\framework\src\Il‌​luminate\Foundation\‌​Auth\RegistersUsers.‌​php=>which uses trait RegistersUsers.

The output in http://localhost:8000/register/store ""Sorry, the page you are looking for could not be found.NotFoundHttpException in RouteCollection.php line 179:"

How to proceed further.


Sign in to participate in this thread!


We'd like to thank these amazing companies for supporting us