use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; use App\Models\UserList; class UserListController extends Controller { public function create(Request $request) { $user = Auth::user(); $list = new UserList; $list->name = $request->input('name'); $list->user_id = $user->id; $list->save(); return response()->json([ 'status' => 'success', 'message' => 'User list created!' ]); } }
use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; use App\Models\User; use App\Models\UserList; class UserListController extends Controller { public function addMember(Request $request) { $user = Auth::user(); $list = UserList::find($request->input('list_id')); $member = User::find($request->input('user_id')); if ($list->user_id != $user->id) { return response()->json([ 'status' => 'error', 'message' => 'You are not authorized to add members to this list!' ]); } $list->members()->attach($member); return response()->json([ 'status' => 'success', 'message' => 'Member added to user list!' ]); } }This code uses the Laravel framework once again to find the UserList object and the User object corresponding to the list and the user being added. It then checks that the current user is authorized to add members to the list, and if so, adds the member to the list by attaching them using the `attach` method. It then returns a JSON response indicating success or failure.