Php 尝试使用Laravel 5.1插入数据时出错
我在尝试使用Laravel5.1插入数据时遇到了麻烦 这是我的密码:Php 尝试使用Laravel 5.1插入数据时出错,php,request,laravel-5.1,relationships,Php,Request,Laravel 5.1,Relationships,我在尝试使用Laravel5.1插入数据时遇到了麻烦 这是我的密码: $event = Event::whereId($request->input('event'))->first(); $participant = new Participant([ 'email' => $request->input('mail'), 'lastname' => $request->input('lastname'), 'firstname'
$event = Event::whereId($request->input('event'))->first();
$participant = new Participant([
'email' => $request->input('mail'),
'lastname' => $request->input('lastname'),
'firstname' => $request->input('firstname'),
'phonenumber' => $request->input('phone'),
'address' => $request->input('address'),
'department' => $request->input('department')
]);
$gender = Gender::whereId($request->input('gender'));
$expertise = Expertise::whereId($request->input('expertise'));
$country = Country::whereId($request->input('country'));
$participant->gender()->associate($gender);
$participant->expertise()->associate($expertise);
$participant->country()->associate($country);
$event->participant()->save($participant);
她是我的错:
"Object of class Illuminate\Database\Eloquent\Builder could not be converted to string"
[...]
at BelongsToMany->save(object(Participant)) in EventController.php line 122
第122行对应于:“$event->participant()->save($participant);”
我想你不需要更多的信息,但你可以随时问我。一个
活动大概有很多参与者。如果是这样的话,我相信你的事件
模型上的方法应该是participants()
,而不是participant()
对于任何来到这里的人,我是这样解决的:
$gender = Gender::whereId($request->input('gender'))->first;
$expertise = Expertise::whereId($request->input('expertise'))->first;
$country = Country::whereId($request->input('country'))->first;
这是我在模型“事件”中的“参与者”关系:公共功能参与者({return$this->belongsomany('App\participant');}我假设ID是您的性别
,专业知识
和国家
模型的主键?为了使它更具可读性,只需执行以下操作:$gender=gender::find($request->input('gender');
$gender = Gender::whereId($request->input('gender'))->first;
$expertise = Expertise::whereId($request->input('expertise'))->first;
$country = Country::whereId($request->input('country'))->first;