-
app/Mail/Team/SendUserInvite.php
Open in GitHubuse Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Mail\Mailables\Address; use Illuminate\Mail\Mailables\Content; use Illuminate\Mail\Mailables\Envelope; use App\Models\Invite; use App\Models\Workspace; class SendUserInvite extends Mailable implements ShouldQueue { use Queueable; use SerializesModels; public function __construct( public Workspace $workspace, public Invite $invite ) { $this->onQueue('emails'); } public function envelope(): Envelope { return new Envelope( from: new Address(config('mail.from.address'), config('mail.from.name')), subject: "You are invited to join the {$this->workspace->name} team.", ); } public function content(): Content { return new Content( view: 'mail.invite', with: [ 'title' => "You are invited to join the {$this->workspace->name} team.", 'workspace' => $this->workspace, 'url' => route('auth.invites.show', $this->invite->id), ], ); } }
-
app/Http/Controllers/Setting/InviteController.php
Open in GitHubuse App\Http\Requests\Invite\InviteRequest; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Gate; use App\Models\User; use App\Models\Invite; use App\Mail\Team\SendUserInvite; use Inertia\Inertia; class InviteController extends Controller { // ... public function store(InviteRequest $request) { $response = Gate::inspect('reached-user-limit', $request->workspace); if (!$response->allowed()) { session()->flash('flash.banner', 'You have reached the limit of team members, please upgrade your plan.'); session()->flash('flash.bannerStyle', 'danger'); return redirect()->route('setting.team-members.index'); } $workspace = auth()->user()->currentWorkspace; // check if email already exist $user = User::where('email', $request->email)->first(); // if user already exists if ($user) { // attach user to project $user->workspaces()->syncWithPivotValues([ $workspace->id ], ['role' => $request->role], false); session()->flash('flash.banner', 'User was added to team!'); session()->flash('flash.bannerStyle', 'success'); return back(); } // create invite $invite = new Invite; $invite->workspace_id = $workspace->id; $invite->email = $request->email; $invite->role = $request->role; $invite->save(); // send email Mail::to($invite->email)->send(new SendUserInvite($workspace, $invite)); session()->flash('flash.banner', 'Invite was sent!'); session()->flash('flash.bannerStyle', 'success'); return back(); } // ... }