我正在努力与hasOne关系。这就是我所拥有的
用户模型:
class User extends Authenticatable
{
use Notifiable;
public function candidat()
{
return $this->hasOne('App\Model1');
}
public function element ()
{
return $this->hasOne('App\Model2');
}
}
Model1类:
class Model1 extends Model
{
//
public function user()
{
return $this->belongsTo('App\User');
}
}
Model2类:
class Model2 extends Model
{
//
public function user()
{
return $this->belongsTo('App\User');
}
}
控制器类:
public function savingcandidat(CandidatRequest $requete){
$candidat = $this->candidatSave($requete);
$user = User::query()->where('email',$requete->email)->get(); //there i get the user
//here is where i get the error : "Method Illuminate\Database\Eloquent\Collection::candidat does not exist."
$user->candidat()->save($candidat);
}
我得到了错误:
“方法Illuminate \ Database \ Eloquent \ Collection :: candidat不 存在。”
当我使用修补匠时,它可以正常工作,在此先感谢您的帮助
答案 0 :(得分:1)
$user = User::query()->where('email',$requete->email)->get();
这将返回收集结果,并且不会具有该模型/生成器方法。如果仅是一条记录,请使用first()代替get
喜欢
$user = User::query()->where('email',$requete->email)->first();