noblemfd
3 weeks ago
131
2
Laravel

How to hide delete button based on condition

Posted 3 weeks ago by noblemfd

I have this code:

public function index()
{
    $userCompany = Auth::user()->company_id;
    $userEmployee = Auth::user()->employee_id;
    
    if (Auth::user()->hasRole('Super Admin')) {
        $respondents            = AppraisalRespondent::all();
        $respondentexternals    = AppraisalRespondentExternal::all();           
    } else {
        $respondents            = AppraisalRespondent::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('company_id', $userCompany)->get();
        $respondentexternals    = AppraisalRespondentExternal::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('company_id', $userCompany)->get();
    }
    return view('appraisal.appraisal_respondents.index')
            ->with('identities', $identities)
            ->with('employees', $employees)                
            ->with('respondents', $respondents)
            ->with('respondentexternals', $respondentexternals);
}

view

                @foreach($respondents as $key => $respondent)
                    <tr>
                        <td>
                            {{$key+1}}
                        </td>
                        <td>
                            {{$respondent->employeerespondent->employee_code}} - {{$respondent->employeerespondent->first_name}} {{$respondent->employeerespondent->last_name }}
                        </td>
                        <td>
                            {{isset($respondent->department) ? $respondent->department->dept_name : 'None'}}
                        </td>
                        <td>
                            {{ $respondent->created_at->format('d-M-Y') }}
                        </td>
                        <td>
                            <button type="button" class="btn btn-sm btn-info mr-1 edit-respondent" data-toggle="modal" data-target="#edit_respondent{{ $respondent->id }}">Edit</button>

                                     <button class="btn btn-xs btn-danger" type="submit" onclick="deleteRespondent({{ $respondent->id }} )">
                                         Delete
                                     </button>
                                     <form id="delete-form-{{ $respondent->id }}" action="{{ route('appraisal.appraisal_respondents.destroy',$respondent->id) }}" method="POST" style="display: none;">
                                         @csrf
                                         @method('DELETE')
                                     </form>                                
                        </td>  
 @endforeach

How do I hide

                                     <button class="btn btn-xs btn-danger" type="submit" onclick="deleteRespondent({{ $respondent->id }} )">
                                         Delete
                                     </button>

where the field email_sent = 1

in

        $respondents            = AppraisalRespondent::all();        
        $respondents            = AppraisalRespondent::where('employee_id', $userEmployee)->where('appraisal_identity_id', $identities->id)->where('company_id', $userCompany)->get();

as it is in the controller

Thanks

Please sign in or create an account to participate in this conversation.