Support the ongoing development of Laravel.io →
Security Eloquent Validation

Hi All,

it's me again, writing new thread to ask for some help..

i have a model, which i called "agent" it is simply just similar to "user" model, which in it's table stored user's data including email and password. and i guarded some field like "password" to prevent mass assignment. But i become a little bit confuse when i have to create a function to enable user to "update their password", that's mean i have to unguard my "agent" model.

since i intent to create rest API controller, i really have to careful with this issue. Here is methods in my controller looks like :

    public function update(Request $request)
    {
        $check = $this->__loginCheck($request);
        if ($check) {
            return $check;
        }

        $validator = Validator::make($request->all(), [

            'agent_fname' => 'required|max:50',
            'agent_lname' => 'required|max:50',
            'agent_email' => 'required|email|max:100',
            'agent_address' => 'required|max:500',
            'agent_cno' => 'required|numeric|digits_between:1,25',
            'agent_birthdate' => 'required|date',
            'agent_wstatus' => 'numeric|digits_between:1,1',
            'region_id' => 'numeric|digits_between:1,4'
        ]);


        if ($validator->fails()) {
            $message = $validator->errors();
            return $this->RespondWithError($message);
        }
        $data = $request->input();
        $updated = $this->__updateAgentData($request, $data, "Your Profile Has Been Updated");
        if ($updated) {
            return $updated;
        }
    }

public function password_update(Request $request)
    {
        $check = $this->__loginCheck($request);
        if ($check) {
            return $check;
        }

        $validator = Validator::make($request->all(), [
            'agent_pass' => 'required|max:50|confirmed',
            'agent_pass_confirmation' => 'max:50'
        ]);

        if ($validator->fails()) {
            $message = $validator->errors();
            return $this->RespondWithError($message);
        }

        $data = $request->input();
        unset($data['agent_pass_confirmation']);
       
        $updated = $this->__updateAgentData($request, $data, "Your Profile Has Been Updated");
        if ($updated) {
            return $updated;
        }
    }

private function __updateAgentData(Request $request, array $update, $message = "Data Successfully Updated")
    {
        $agent = $request->session()->get('agent.0');

        $saved = agents::find($agent->agent_id)
            ->update($update);

        $arr_agent = array_merge((array)$agent, $update);
        $object = new agents($arr_agent);
        $this->__storeSessionAgent($request, $object);

        if ($saved) {
            $data = $this->RespondUpdated($message, 1, 1);
            $save = array_merge($arr_agent, $data);
            $this->accessLog($request, json_encode($save));
            return $this->RespondUpdated($message);
        }
        return false;
    }

and here is my agent model :


class agents extends Model
{
    protected $table ='tb_agents';
    protected $primaryKey = 'agent_id';
    public $timestamps = false;
    protected $guarded = ['agent_id','agent_no','agent_pass'];
}

can i just disable "guarded" only on certain field ( ie : agent_pass ) while password_update is running and enable it again once it finish ( the password field is updated ). Because if i just wite this :

agents::unguard();

it unguard all "guarded field". in short word, i want to unguard "agent_pass" field while i am updating password but still guarded others to prevent mass assignment.

can somebody help me..

thanks in advance.

regards

Last updated 3 years ago.
0

why dont you in the controller mass assign all the other fields and then assign the password/guarded field manually underneath?

0

@shez1983 thank you for reply. but how to assign guarded field manually?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.