dongxi7704
2015-10-02 17:08
浏览 70
已采纳

使用Laravel 5.1在导航栏中显示userRoleName

I'm new in MVC systems. I'm new user in laravel.

I have a simple problem. I read laravel documentation. But i couldn't understand MVC system exactly.

Model : App\Models\User Controller : App\Http\Controllers\AuthController View : template/navbar.blade.php

Model

    <?php

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
    AuthorizableContract,
    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    protected $table = 'users';

    public function userRole(){
        return $this->user_role;
    }
}

Controller

<?php

namespace App\Http\Controllers;

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

class AuthController extends Controller
{
    public function getLogin(){
        return view('auth.login');
    }

    public function postLogin(Request $request){
        $this->validate($request, [
            'mail' => 'required',
            'password' => 'required',
        ]);

        if(!Auth::attempt($request->only(['mail', 'password']))){
            return redirect()->back()->with('form-error', trans('auth.failed'));
        }

            return redirect()->back()->with('info', "giriş yapıldı");
    }

    public function getLogout(){
        Auth::logout();
        return redirect()->back();
    }

    public function getRegister(){

    }

    public function postRegister(){

    }

    public function userRoleName(){

        $userRole = User::userRole();

        switch ($userRole){
            case 9: return "Üst Düzey Yönetici"; break; //superAdmin
            case 8: return "Yönetici"; break;           //admin
            case 7: return "Yardımcı Yönetici"; break;  //moderator
            case 6: return "Muhasebe Bölümü"; break;    //accounting
            case 5: return "Satış Bölümü"; break;       //marketing
            case 4: return "Depo Bölümü"; break;        //storage
            case 3: return "Ürün Yönetimi"; break;      //productManager
        }
    }
}

I want to show userRoleName in navbar (in views)

<li><a>{{ Auth::user()->name }}<br/><em>{{ -- CONTROLLER METHOD RESULT HERE --- }}</em></a></li>

How can i do that ? I'm using laravel 5.1

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjing3656 2015-10-03 11:08
    已采纳

    Create a function called getUserRoleAttribute in your User-Model like this:

    public function getUserRoleAttribute(){
        switch ($this->user_role){
            case 9: return "Üst Düzey Yönetici"; break; //superAdmin
            case 8: return "Yönetici"; break;           //admin
            case 7: return "Yardımcı Yönetici"; break;  //moderator
            case 6: return "Muhasebe Bölümü"; break;    //accounting
            case 5: return "Satış Bölümü"; break;       //marketing
            case 4: return "Depo Bölümü"; break;        //storage
            case 3: return "Ürün Yönetimi"; break;      //productManager
        }
    }
    

    You can remove your other userRole-function in your model and controller entirely and just access this attribute like you access the name in your view:

    Auth()->user()->user_role
    

    This is called an accessor (link to Laravel Docs)

    点赞 打赏 评论

相关推荐 更多相似问题