auth:: user()未定义的方法'user'

问题描述 投票:0回答:1
<?php namespace App\Http\Controllers; use Illuminate\Container\Attributes\Auth; use Illuminate\Foundation\Auth\User as AuthUser; class DashboardController extends Controller { public function index(){ /* $user = Auth::user(); */ $user = Auth::user(); $tasks = Auth::user()->tasks; return view ('dashboard', compact('tasks')); } }

dashboard.blade.php

<x-app-layout> <x-slot name="header"> <h2 class="font-semibold text-xl text-gray-800 leading-tight"> {{ __('Dashboard') }} </h2> </x-slot> <div class="py-12"> <div class="max-w-7xl mx-auto sm:px-6 lg:px-8"> <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg"> <div class="p-6 text-gray-900"> @foreach (Auth::user()->tasks as $task) <p> {{ $task->title }}</p> <p> {{ $task->description }}</p> @endforeach </div> </div> </div> </div> </x-app-layout>

用户表
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
            //add deleted_at column
            $table->softDeletes();
        });

用户模量
<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{

    use HasFactory, Notifiable;
    use SoftDeletes;

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

    protected $hidden = [
        'password',
        'remember_token',
    ];


    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }


    public function tasks(): HasMany
    {
        return $this->hasMany(Task::class);
    }

}

web.php(路由:: get是我要实现的目标)

<?php

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

Route::redirect('/','/dashboard');

Route::get('/dashboard', [DashboardController::class, 'index'])
->middleware(['auth'])
->name('profile');

Route::view('profile', 'profile')
    ->middleware(['auth'])
    ->name('profile');

require __DIR__.'/auth.php';

请帮助我只是一个实习生

	

你在进口错误的班级。

use Illuminate\Container\Attributes\Auth;
应该是

use Illuminate\Support\Facades\Auth;

laravel routes controller laravel-blade laravel-11
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.