我使用 laravel 11,带有 breeze 入门套件和 Livewire(volt 类 api)和 alpine。
在名为“DashboardController”的控制器中,我尝试使用“Auth::user”来呈现包含用户任务的视图,但它无法识别“user”,我之前已经做过几次了,但从未遇到过此错误。最奇怪的是,在 blade 文件中它确实识别了 Auth::user,所以目前我不知道还能做什么,我还没有发现有其他人遇到此问题。
仪表板控制器
<?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'));
}
}
仪表板.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(route::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';
请帮助我,我只是一名实习生
您导入了错误的类。
应该是
去
Auth::user()
工作。它之所以在 blade 中起作用是因为当 Blade 编译器编译视图时
Illuminate\Support\Facades\Auth
它被别名化。Auth
您正在使用的类是用于解析 Guard 的上下文属性。
使用方法如下。这两个块是等效的。
或者,您可以
Authenticated
在用例中使用上下文属性。以下是有关此事的一些文件,仅是相关部分。
导入正确的 Auth Facade
在 DashboardController.php 文件中,替换此错误的导入:
正确的是:
然后你的index()函数应该可以工作了。
为什么会发生这种情况?
您错误地导入了
Illuminate\Container\Attributes\Auth
,这不是正确的 Auth 外观。Blade 模板(dashboard.blade.php)可以识别
Auth::user()
,因为 Blade 自动提供对外观的访问。
但是,Controller 需要正确的命名空间
Illuminate\Support\Facades\Auth
。