Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 尝试使用Laravel 5.1插入数据时出错_Php_Request_Laravel 5.1_Relationships - Fatal编程技术网

Php 尝试使用Laravel 5.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'

我在尝试使用Laravel5.1插入数据时遇到了麻烦

这是我的密码:

$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;