mercredi 13 septembre 2017

Laravel 5.4, reusing a if else statement throughout site

I found myself needing to repeat the if else statement throughout the site because I have two url's. One is for admin, one is for users. Do I have to separate out the controllers to stop this or is there a better way to do this?

URL structure :
Admin : /buildings/{building_id}/tenants
Users : /tenants


TenantsController.php

public function index(Request $request, $building_id = null)
{

   if($request->user()->hasRole('admin')){
        $building_id = $building_id;
   }else{
        $building_id = Auth::user()->building_id;
   }

   $building = Building::findOrFail($building_id);

   $tenants = User::whereHas('roles', function($q){$q->whereIn('name', ['tenant']);})->where('building_id',$building_id)->paginate(2);

   return view('buildings.tenants.index')->withTenants($tenants)->withBuilding($building);
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire