Laravel 9 jetstream , I need to sanitize this Controllers is there away to do that? [closed]

I just started my first Laravel project and create an 'authorized waiting view' for role users, using laravel-9 jetstream and custom-made middleware. Is there a way to reduce the number of codes that have been repeated again and again?

  • are there artisan commands to do that?
  • what is the best way to reduce code duplication.

 $name = $authorizeRequestUser->name;
 $contact_number = $authorizeRequestUser->contact_number;
 $email = $authorizeRequestUser->email;
 $massage = 'Please Contact bellow persion to Authorize your login';

check the repeating code above.

please let me know if there is a specific way to do that.

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class AuthorizeWaitingController extends Controller
{
    public function index() {
        $name = 'John Doe';
        $contact_number = '07xxxxxxx';
        $email = 'jonhdoe@gmail.com ';
        $massage = 'Please Contact bellow persion to Authorize your login';
        
        if (auth()->user()->role_id === 2){
            $authorizeRequestUser = User::where('role_id',1)->first();

           if ($authorizeRequestUser !== null) {
                $name = $authorizeRequestUser->name;
                $contact_number = $authorizeRequestUser->contact_number;
                $email = $authorizeRequestUser->email;
                $massage = 'Please Contact bellow persion to Authorize your login';
           } 

        }else if(auth()->user()->role_id === 3){
            $authorizeRequestUser = User::where('role_id',2)->where('center_id', auth()->user()->center_id)->first(); //OrFail
          
            if($authorizeRequestUser === null){
                $authorizeRequestUser = User::where('role_id',1)->first();
                if ($authorizeRequestUser !== null) {
                    $name = $authorizeRequestUser->name;
                    $contact_number = $authorizeRequestUser->contact_number;
                    $email = $authorizeRequestUser->email;
                    $massage = 'Please Contact bellow persion to Authorize your login';
                }
            }else{
                $name = $authorizeRequestUser->name;
                $contact_number = $authorizeRequestUser->email;
                $massage = $authorizeRequestUser->contact_number;
                $email  = 'Please Contact bellow persion to Authorize your login';
            }
           
        }
        
        return view('components.authorize-waiting', compact('name', 'contact_number', 'email', 'massage'));

    }
}

```````````````````````````````````````````````````


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation