skip to Main Content

Hi everyone i have a question how can i display the subject based on the user status. if the user is active which means the the full subject will be displayed but if he is not active i only want to display specific subject.

 public function inquirySubject()
    {
       if(auth()->user()->status === 'approved' || auth()->user()->status === 'active')
         {
            $subject = [
            'Inquiry',
            'Follow Up',
            'Technical Problem',
            'Other Application Concern',
            'Payment Concern' ,
            'Proof of Payment'
        ];
        }else{
            $subject = [
            'Inquiry',
            'Follow Up',
            'Technical Problem',
            'Other Application Concern',
            // 'Payment Concern' , // if user is not active or approved this subject line should not     display 
            'Proof of Payment'
        ];
        }
        return response()->json($subject);
    }

2

Answers


  1. You could do something like this to save a bit of code duplication:

     public function inquirySubject()
     {
         $isApprovedOrActiveUser = auth()->user()->status === 'approved' || auth()->user()->status === 'active';
            
         $subject = [
             'Inquiry',
             'Follow Up',
             'Technical Problem',
             'Other Application Concern',
             'Proof of Payment'
         ];
     
         if ($isApprovedOrActiveUser) {
            $subject[] = 'Proof of Payment';
         }
    
         return response()->json($subject);
    }
    

    You could also implement this logic in a Resource – see: https://laravel.com/docs/9.x/eloquent-resources

    Login or Signup to reply.
  2. Add to model

        public const SUBJECT_ACTIVE_OR_APPROVED = [
             'Inquiry',
             'Follow Up',
             'Technical Problem',
             'Other Application Concern',
             'Payment Concern',
             'Proof of Payment',
         ]; 
    

    And

    public const SUBJECT_NOTACTIVE = [
             'Inquiry',
             'Follow Up',
             'Technical Problem',
             'Other Application Concern',
             'Proof of Payment',
         ];
    

    Your function

    public function inquirySubject()
        {
            return (auth()->user()->status === 'approved' || auth()->user()->status === 'active')
                ? response()->json(Model::SUBJECT_ACTIVE_OR_APPROVED)
                : response()->json(Model::SUBJECT_NOTACTIVE);
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search