接下來是ProjectInvitationController:
class ProjectInvitationController extends Controller
{
public function store(InvitationRequest $request, Project $project)
{
$validated = $request->validated();
$invitedMember = User::whereEmail($validated['email'])->first();
Mail::to($invitedMember->email)->send(new InvitationInformed($invitedMember, $project));
$project->invite($invitedMember);
return redirect(route('project.show', compact('project')));
}
}
class InvitationRequest extends FormRequest
{
protected $errorBag = 'invitation';
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Gate::allows('invite',$this->route('project'));
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'email'=>'required|exists:users,email'
];
}
public function messages()
{
return [
'email.exists'=>'The invited member should have a valid familyboard account'
];
}
}