Laravel11 博客1--使用Laravel Jetstream | 多用户和管理员登录

243 阅读1分钟

安装Jetstream

composer require laravel/jetstream

使用Livewire安装Jetstream

php artisan jetstream:install livewire

安装Jetstream后,您应该安装和构建NPM依赖关系并迁移数据库:

npm install

npm run build

php artisan migrate

database/migrations/0001_01_01_000000_create_users_table.php 迁移文件添加phone和usertype字段

Schema::create('users', function (Blueprint $table) {

            $table->string('phone')->nullable();
            $table->string('usertype')->default('user'); // 用户默认值,当用户登录,会将其带到用户模版
           
        });

app/Models/User.php user模型设置白名单,添加phone和usertype字段

protected $fillable = [
        'phone',
        'usertype',
    ];

resources/views/auth/register.blade.php 注册模版添加'phone'字段

<div class="mt-4">
                <x-label for="email" value="{{ __('手机号') }}" />
                <x-input id="email" class="block mt-1 w-full" type="text" name="phone" :value="old('phone')" required autocomplete="username" />
            </div>

app/Actions/Fortify/CreateNewUser.php 修改控制器

return User::create([
           
            'phone' => $input['phone'],
          
        ]);

效果

image.png

多用户和管理员登录

注册 用户和 管理员账号

image.png 数据库去修改一下 admin 账号的usertype类型为admin

控制器创建

创建 管理员 控制器

php artisan make:controller HomeController
<?php

namespace App\Http\Controllers;

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

class HomeController extends Controller
{
    //
    public function index()
    {
        if(Auth::id())
        {
            $usertype = Auth()->user()->usertype;

            if($usertype=='user')
            {
                return view('dashboard');
            }
            else if($usertype=='admin')
            {
                return view('admin.index');
            } else {
                return redirect()->back();
            }

        }

    }
}

路由

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;


Route::get('/home',[HomeController::class,'index'])->name('home');

自定义跳转

config/fortify.php 登录尝试成功,Fortify 会将用户重定向到通过应用程序的 fortify 配置文件中的 home 配置选项配置的 URI

'home' => '/home',

前端模版

resources/views/admin/index.blade.php

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('管理员 主页') }}
        </h2>
    </x-slot>

    123
</x-app-layout>

修改前端模版路由跳转

resources/views/welcome.blade.php

@auth
    <a
        href="{{ url('/home') }}"
        class="rounded-md px-3 py-2 text-black ring-1 ring-transparent transition hover:text-black/70 focus:outline-none focus-visible:ring-[#FF2D20]"
    >
        Dashboard
    </a>

href="{{ url('/home') }}" 为修改的路由跳转

resources/views/navigation-menu.blade.php

<!-- Logo -->
<div class="shrink-0 flex items-center">
    <a href="{{ route('home') }}">
        <x-application-mark class="block h-9 w-auto" />
    </a>
</div>


<!-- Navigation Links -->
<div class="hidden space-x-8 sm:-my-px sm:ms-10 sm:flex">
    <x-nav-link href="{{ route('home') }}" :active="request()->routeIs('home')">
        {{ __('Dashboard') }}
    </x-nav-link>
</div>


<!-- Responsive Navigation Menu -->
<div :class="{'block': open, 'hidden': ! open}" class="hidden sm:hidden">
    <div class="pt-2 pb-3 space-y-1">
        <x-responsive-nav-link href="{{ route('home') }}" :active="request()->routeIs('home')">
            {{ __('Dashboard') }}
        </x-responsive-nav-link>
    </div>

home为修改的路由跳转

管理员后台效果:

image.png

用户后台效果:

image.png