lundi 21 décembre 2020

Laravel loop undefined variable in loop

I am trying to redirect users to a page where they can choose facilities they belong to. Super admins can view all facilities, this works perfectly. Organization admins should only view facilities they belong to. Unfortunately, this part fails, and i get an error Undefined variable: facilities despite the fact that facilities is defined, and if i run the quary, the correct result is obtained. The third part of the loop gets executed by default. Any tips/advice on what i am doing wrong in my loop will be appreciated.

group = $user->groups;            

        if($group[0]->name === 'Super Administrator') {

            $facilities = Facility::all();

        } else if($group[0]->name === 'Organization Administrator') {
        
            $org_id = $group[0]->organization_id;
            
            $facilities = Facility::where('organization_id', '=', $org_id)->get();

        } else if( (!empty($group[0]->facility)) && $group[0]->facility->count() == 1) {

            $url = $scheme . $facility->first()['subdomain'] . '.' . env('APP_DOMAIN', 'localhost.loc');

            return redirect()->away($url);

        } 

        $facilities = $facilities->unique();

The value of $group is :

  {
    "uuid": "924bd268-f97e-4570-9122-b30a6b23cf8a",
    "name": "Test Organization Admin",
    "alias": "test-organization-admin",
    "description": "Organization Administrator",
    "facility_id": 2,
    "status": 1,
    "facility": {
      "uuid": "924bcf28-63a8-4541-b54f-e480e3a7aae0",
      "name": "Test Facility",
      "organization_id": 4,
      "subdomain": "testfacility",
      "schema_name": "testfacility_2020_12_21_122917",
      "status": 1,
      "logoWeb": null,
      "logoWebMenu": null
    }
  }
]                                                                                                    ```


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire