Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MSalah's avatar

Call to undefined method Illuminate\Database\Query\Builder::associate()

User Model

public function profile(){
    return $this->hasOne('App\Profile');
}

Profile Model

public function User(){
    return $this->belongsTo('User');
}

Update Controller

$students = User::findOrFail($id);
    $students->name = $request->get('name');
    $students->email = $request->get('email');
    $students->password = $request->get('password');

    if($request->hasFile('image'))
    {
        $image = $request->file('image');
        $name = time().$image->getClientOriginalName();
        $image->move('images/profiles', $name);
        $students->image = $name;
    }
    $students->save();

    $profile = Profile::updated($profileRequest->all());
    $students->Profile()->associate($profile);
    $profile->save();
                

ERRORR

Call to undefined method Illuminate\Database\Query\Builder::associate()

0 likes
7 replies
bobbybouwmann's avatar

I think it should be profile() instead of Profile()

And I think you also need to use save for one-to-one relations

$profile = Profile::create(['field1' => 'value1']);
$students->profile()->save($profile);
d3xt3r's avatar

How is your schema defined. AFAIKhasOne doesn't have an associate() method. Use inverse relationship if possible.

thomaskim's avatar

When updating a belongsTo relationship, you may use the associate method.

You did it the opposite way. It should be:

$profile = Profile::create($profileRequest->all()); // But if you are updating a profile, check the answer below (4 or so posts down)
$profile->user()->associate($students);
$profile->save();

Some other things I would recommend or are confused about...

It doesn't seem to make sense to use a plural variable ($students) when singular seems to make more intuitive sense.

We don't know what $profileRequest is referring to.

Finally, why are you using the updated method? Shouldn't it be update?

MSalah's avatar

@thomaskim

I use your code and give me this error

Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically, assuming $this from incompatible context

$profileRequest referring to profile table

this is update controller

public function update( $id,  Requests\CreateUserRequest $request,  Requests\CreateProfileRequest $profileRequest ){
$students = User::findOrFail($id);
    $students->name = $request->get('name');
    $students->email = $request->get('email');
    $students->password = $request->get('password');

    if($request->hasFile('image'))
    {
        $image = $request->file('image');
        $name = time().$image->getClientOriginalName();
        $image->move('images/profiles', $name);
        $students->image = $name;
    }
    $students->save();

    $profile = Profile::update($profileRequest->all());
    $profile->user()->associate($students);
    $profile->save();

    $students->assignRole('student');
    $students->save();


    return redirect('students');
}
2 likes
thomaskim's avatar

Whoops. That was a dumb mistake by me. Does the student already have a profile or are you creating a new one?

thomaskim's avatar
Level 41

If the student already has a profile, you could prob just shrink it to one line of code and do this.

$students->profile->update($profileRequest->all());

Please or to participate in this conversation.