douzhan8395 2019-01-07 08:23
浏览 55
已采纳

如何创建管理员而不创建管理表到数据库中,并将一个登录路由和控制器放入同一页面?

I have two controller classes one for custom signin and one for my items.
Custome controller class that authenticates all the users

class SigninController extends Controller
{
    public function getSigninRequest(Request $request)
    {


        $this->validate($request,[
               'email'=>'required|email',
               'password'=>'required'
        ]);

        if(Auth::attempt(['email'=>$request->input('email'),
                          'password'=>$request->input('password')],
                           $request->has('remember')))
        {
            return redirect()->route('admin.index');
        }
        return redirect()->back()->with('fail','Authentication failed!');
    }


}

User Model class for normal users and admin

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    const ADMIN_TYPE = 'admin';
    const DEFAULT_TYPE ='default';

    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function books()
    {
        return $this->hasMany('App\Book');
    }
      public function isAdmin()
      {
          return $this->type=== self::ADMIN_TYPE;
      }


}
 user database migration in which I added type row
  public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('type')->default('default');
            $table->rememberToken();
            $table->timestamps();
        });
    }

Route web.php. I want to pass all data with one login route

Auth::routes();

Route::post('login',[
            'uses'=>'SigninController@getSigninRequest',
            'as'=>'auth.signin'
]);

I want to create admin user who can add/delete and edit items even those items are created by normal users who can only add/edit and delete their own items only. So, after login, all users must be redirected to the same page.

  • 写回答

2条回答 默认 最新

  • drccfl9407 2019-01-07 10:24
    关注

    we believe you have a table in database that it's name in Users and model User and it has 3 columns with this values

    id,    name,    password,    role
    1      jack     123456       admin
    2      bob      123456       user
    

    so you have a form that should login just with name and you have a controller with name UserController

    this is the update function

    public function update(Request $request)
    {
        $user = /Auth::guard('your_guard')->user();
        if($user->role == 'admin')
        {
           //update rows from everyOne, because this user is admin
           $all_users_information = User::all();     //you let him do everyThing with all users table
        }
        elseif($user->role != 'admin')
        {
            //update just himself information and don't let him to access others informations
            $just_your_information = User::find($user->id);    //do something with just himself information
        }
    }
    

    i hope you can use it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用