Last active
January 29, 2020 14:06
-
-
Save AnowarCST/f32e7bf2242564c12575005987142f31 to your computer and use it in GitHub Desktop.
Sample ProfileController for Medium Post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\API\V1; | |
use App\Http\Controllers\Controller; | |
use App\Http\Requests\Users\ChangePasswordRequest; | |
use App\Http\Requests\Users\ProfileUpdateRequest; | |
use App\Models\User; | |
use Illuminate\Support\Facades\Hash; | |
class ProfileController extends Controller | |
{ | |
/** | |
* Create a new controller instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->middleware('auth:api'); | |
} | |
/** | |
* Return the user data | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function profile() | |
{ | |
$response = [ | |
'success' => true, | |
'data' => auth('api')->user(), | |
'message' => 'User Profile', | |
]; | |
return response()->json($response, 200); | |
} | |
/** | |
* Update the profile by users | |
* | |
* @param \App\Http\Requests\Users\ProfileUpdateRequest $request | |
* | |
* @return \Illuminate\Http\Response | |
* @throws \Illuminate\Validation\ValidationException | |
*/ | |
public function updateProfile(ProfileUpdateRequest $request) | |
{ | |
$user = auth('api')->user(); | |
$user->update($request->all()); | |
$response = [ | |
'success' => true, | |
'data' => $user, | |
'message' => 'Profile has been updated', | |
]; | |
return response()->json($response, 200); | |
} | |
/** | |
* Update the specified resource in storage. | |
* | |
* @param \App\Http\Requests\Users\ChangePasswordRequest $request | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function changePassword(ChangePasswordRequest $request) | |
{ | |
User::find(auth('api')->user()->id)->update(['password' => Hash::make($request->new_password)]); | |
$response = [ | |
'success' => true, | |
'data' => [], | |
'message' => 'Password Has been updated', | |
]; | |
return response()->json($response, 200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment