public function modifyCustomList($listId = null) { $messages = ['required' => 'The :attribute field is required.', 'numeric' => 'The :attribute field is required.', 'max' => 'List Name is limited to :max characters.']; $validator = Validator::make(Input::all(), ['title' => 'required|max:30', 'privacy' => 'required|numeric'], $messages); if ($validator->fails()) { return ['error' => $validator->errors()->all()[0]]; } $user = Auth::user(); if (!is_null($listId)) { $userList = UserList::where('id', $listId)->first(); if ($userList->user_id !== $user->id) { return ['error' => 'You do not have permission to edit this list']; } } else { if (!$user->canMakeList()) { return ['error' => 'You\'ve reached the limit of list you can create!']; } else { $userList = new UserList(); } } $userList->title = Input::get('title'); $userList->privacy = Input::get('privacy'); if (!is_null($listId) && !$userList->save() || !$user->UserList()->save($userList)) { return ['error' => 'There was an error while trying to save the list.']; } $myLists = UserList::where('user_list.user_id', $user->id)->leftjoin('user_list_profile as ulp_1', 'ulp_1.user_list_id', '=', 'user_list.id')->groupBy('user_list.id')->orderBy('user_list.id', 'desc')->leftJoin('subscription', function ($join) { $join->on('subscription.user_list_id', '=', 'user_list.id')->whereNull('subscription.deleted_at'); })->whereNull('ulp_1.deleted_at')->get(['user_list.id', 'user_list.title', 'user_list.privacy', 'user_list.created_at', \DB::raw('count(ulp_1.id) as users_in_list'), \DB::raw('count(distinct subscription.id) as sub_count')]); $return = []; foreach ($myLists as $myList) { $return[] = ['id' => $myList->id, 'title' => $myList->title, 'privacy' => $myList->privacy, 'created_at' => $myList->created_at->format("M j Y"), 'users_in_list' => $myList->users_in_list, 'sub_count' => $myList->sub_count]; } return $return; }