Sunday, April 16, 2017

How to store image path of storage\app\public in laravel-5.4

I am using laravel-5.4 make:auth. In register.blade.php ,added one extra field->profile picture for user .

<form class="form-horizontal" role="form" method="POST" action=""   enctype="multipart/form-data">
                    
                    <div class="form-group">
                        <label for="image" class="col-md-4 control-label"> Profile picture</label>

                        <div class="col-md-6">
                            <input id="image" type="file" class="form-control" name="image">

                            @if ($errors->has('image'))
                                <span class="help-block">
                                    <strong></strong>
                                </span>
                            @endif
                        </div>
                    </div>

I want to store the image path in database.Also I have executed: php artisan storage:link and the [public/storage] directory has been linked.

In app\Http\Controllers\Auth\RegisterController.php:

    public function store(request $request)
{

    if($request->hasFile('image')) {

       $image_name = $request->file('image')->getClientOriginalName();              
        $image_path = $request->file('image')->store('public'); 
        $image = Image::make(Storage::get($image_path))->resize(320,240)->encode();
        Storage::put($image_path,$image);
        $image_path = explode('/',$image_path);

        $user->image = $image_path;
        $user->save();

    } else{

        return "No file selected";

    }       

}

and web.php

Route::post('/store', 'RegisterController@store');
Route::get('/show', 'RegisterController@show');

In database,in user table under image is stored as a temporary path :C:\xampp\tmp\phpC762.tmp. How to store image path of storage\app\public.



via Raja

Advertisement