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()
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);
How is your schema defined. AFAIKhasOne doesn't have an associate() method. Use inverse relationship if possible.
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?
@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');
}
Whoops. That was a dumb mistake by me. Does the student already have a profile or are you creating a new one?
@thomaskim
student already have a profile and i want update this profile
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 sign in or create an account to participate in this conversation.