我有两个模型TeamMember
和ProjectRequest
.
ATeamMember
可以有一个ProjectRequest
,这就是为什么我创建了以下 Eloquent 关系TeamMember
:
class TeamMember extends Model {
//
protected $table = 'team_members';
protected $fillable = ['project_request_id'];
// Relations
public function projectTeam() {
return $this->hasOne('\App\Models\ProjectRequest', 'project_request_id');
}
}
在我的控制器中,我想查询两个表,但是它返回失败消息。
重要的是要知道这$request->projectTeam
是一组电子邮件,如下所示:
array:2 [
0 => "mv@something.com"
1 => "as@something.com"
]
这意味着我需要为数组中电子邮件所在的每个团队成员批量插入team_members
表格。project_request_ id
我怎样才能以正确的方式做到这一点?以下是我的尝试:
public function createProjectTeam(Request $request){
try {
$title = $request->projectTitle;
$TeamMember = $request->projectTeam;
$projectRequest = ProjectRequest::create(['project_title' => $title]);
$projectRequestId = $projectRequest->id;
$projectTeam = $this->teamMembers->projectTeam()->create(['project_request_id'=> $projectRequestId])->where('email', $TeamMember);
//$projectTeam = TeamMember::createMany(['project_request_id' => $projectRequestId])->where($TeamMember);
//dd($projectTeam);
return $projectRequest.$projectTeam;
} catch(\Exception $e){
return ['success' => false, 'message' => 'project team creation failed'];
}
}