anbu369's avatar

Lumen API leaves password field blank in database

I am using Lumen framework to create an API. I created the UserController.php to validate my user's email and password. In the User Model, I declared the password, create_at and updated_at fields as hidden and id, email_id and name field as fillable. When I do a post call to the API, my email and password are validated properly (if I dont send a password, the API throws an error message as expected) but in the back-end database, the password field is left empty but an entry is created for the user for all the other fields including timestamps. Although when I add the password field to the fillable variable, the hashed password gets stored in the database but I would like to do it as a hidden variable. I am new to php and databases so please let me know if this has been answered before as I couldn't find the solution anywhere.

UserController.php

<?php
namespace App\Http\Controllers;

use App\User;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller{

//List all the users
  public function index(){
    $users = User::all();
    return response()->json(['data' => $users], 200);
  }
//Store a new user
  public function store(Request $request){
    $this->validateRequest($request);

    $user = User::create([
      'email_id' => $request->get('email_id'),
      'first_name' => $request->get('firstName'),
      'last_name' => $request->get('lastName'),
      'username' => $request->get('userName'),
      'password' => Hash::make($request->get('password'))
      ]);

      return response()->json(['data' => "The user with id {$user->id} has been created"], 201);
  }

  public function show($id){
    $user = User::find($id);

    if(!$user){
      return respose()->json(['message' => "The user with {$id} doesn't exist"], 404);
    }

    return response()->json(['data' => $user], 200);
  }

  public function validateRequest(Request $request){
    $rules = [
      'email_id' => 'required|email|unique:users',
      'password' => 'required|min:6'
    ];
    $this->validate($request, $rules);
  }
}
?>

User.php

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable;

    protected $fillable = [
        'id', 'email_id', 'first_name', 'last_name', 'username'
    ];

    protected $hidden = [
        'created_at', 'updated_at', 'password'
    ];
}

?>
0 likes
2 replies
ouhare's avatar
ouhare
Best Answer
Level 24

You have to add "password" in the "fillable" array.

The hidden array is use to "hide" fields when you serialize the model (to Array or Json); so you can keep "password" in.

1 like
anbu369's avatar

That was easy:) thanks. I was following a tutorial and they did not mention that. Thanks much

Please or to participate in this conversation.