Laravel 8: 尝试在 null 上读取 属性 "id"
Laravel 8: Attempt to read property "id" on null
我在 Laravel 8 中遇到此错误 'Attempt to read property "id" on null'。之前它工作正常,但在我看来我将 $user->id 更改为 $user->profile->id 现在正在发生这种情况。我登录了应用程序,并相应地更改了我的路线以匹配配置文件 ID,我还尝试清除缓存等。
这是我的代码:
用户模型:
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
// protected $fillable = [
// 'name',
// 'email',
// 'password',
// ];
protected $table = 'users';
protected $guarded = [];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
public function posts ()
{
return $this->hasMany(Post::class);
}
public function profile()
{
return $this->hasOne(Profile::class);
}
}
个人资料模型:
class Profile extends Model
{
use HasFactory;
protected $table = 'profiles';
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
}
配置文件控制器:
class ProfilesController extends Controller
{
public function show(User $user)
{
return view ('profiles.index', compact('user'));
}
public function edit(User $user)
{
return view ('profiles.index', compact('user'));
}
}
路线:
Route::get('profile/{profile}', [ProfilesController::class, 'show'])->middleware('auth');
Route::get('profile/{profile}/edit', [ProfilesController::class, 'edit'])->middleware('auth');
查看:
<x-layout>
<section class="py-8 max-w-4xl mx-auto">
<h1 class="text-lg font-bold mb-8 pb-2 border-b">
@if ($user->id == auth()->user()->id)
Hello {{ $user->name }}, welcome to your profile.
@else
{{ $user->name }}'s Profile.
@endif
</h1>
<div class="flex">
<aside class="w-48 flex-shrink-0">
<h4 class="font-semibold mb-4">
Navigation
</h4>
<ul style="max-width: 75%">
<li>
<a href="/profile/{{ $user->profile->id }}" class="{{ request()->is('profile/'.$user->profile->id) ? 'text-blue-500' : '' }}">View Profile</a>
</li>
<li>
@if ($user->id == auth()->user()->id)
<a href="/profile/{{ $user->profile->id }}/edit" class="{{ request()->is('profile/'.$user->profile->id.'/edit') ? 'text-blue-500' : '' }}">Edit Profile</a>
@endif
</li>
</ul>
</aside>
<main class="flex-1">
<x-panel>
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<div class="flex flex-row grid-cols-12">
<div class="flex flex-col col-span-4 row-span-full justify-items-start flex-grow-0 flex-shrink-0 grid-cols-4">
<div class="flex flex-row">
<img src="http://i.pravatar.cc/60?u={{ $user->profile->id }}" alt="" width="" height="" class="rounded-full h-24 w-24 flex m-2">
</div>
</div>
<div class="flex flex-col col-span-8 justify-right grid-cols-8 text-sm">
<div class="flex flex-row">
<div class="flex flex-col col-span-2 font-semibold">
<div class="pt-3">Name:</div>
<div class="pt-3">Email:</div>
<div class="pt-3">About:</div>
</div>
<div class="flex flex-col col-span-10">
<div class="pt-3 pl-4 text-justify">{{ $user->profile->name }}</div>
<div class="pt-3 pl-4 text-justify">{{ $user->profile->email }}</div>
<div class="pt-3 pl-4 text-justify"><p>{{ $user->profile->description }}</p></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</x-panel>
</main>
</div>
</section>
我认为问题在于您正在传递路线指示的 Profile
模型,但该方法正在寻找 User
模型。
Show 和 Edit 有一个 User
模型作为参数。如果您传递 Profile
id
,该方法将通过 id
找到 User
模型,但使用 Profile
模型的 id
和不是 Profile
模型的 user_id
。
您需要将方法更改为:
public function show(Profile $profile)
{
$user = $profile->user;
return view ('profiles.index', compact('user'));
}
public function edit(Profile $profile)
{
$user = $profile->user;
return view ('profiles.index', compact('user'));
}
使用此代码找到 Profile
模型,并通过关系获取 User
并将其传递给视图。
public function edit($id)
{
$table = Table::find($id);
return view('tables.edit', compact('table'));
}
public function update(Request $request, $id)
{
$table = Table::find($id);
$table->update($request->all());
return redirect('/tables');
}
我在 Laravel 8 中遇到此错误 'Attempt to read property "id" on null'。之前它工作正常,但在我看来我将 $user->id 更改为 $user->profile->id 现在正在发生这种情况。我登录了应用程序,并相应地更改了我的路线以匹配配置文件 ID,我还尝试清除缓存等。
这是我的代码:
用户模型:
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
// protected $fillable = [
// 'name',
// 'email',
// 'password',
// ];
protected $table = 'users';
protected $guarded = [];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
public function posts ()
{
return $this->hasMany(Post::class);
}
public function profile()
{
return $this->hasOne(Profile::class);
}
}
个人资料模型:
class Profile extends Model
{
use HasFactory;
protected $table = 'profiles';
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
}
配置文件控制器:
class ProfilesController extends Controller
{
public function show(User $user)
{
return view ('profiles.index', compact('user'));
}
public function edit(User $user)
{
return view ('profiles.index', compact('user'));
}
}
路线:
Route::get('profile/{profile}', [ProfilesController::class, 'show'])->middleware('auth');
Route::get('profile/{profile}/edit', [ProfilesController::class, 'edit'])->middleware('auth');
查看:
<x-layout>
<section class="py-8 max-w-4xl mx-auto">
<h1 class="text-lg font-bold mb-8 pb-2 border-b">
@if ($user->id == auth()->user()->id)
Hello {{ $user->name }}, welcome to your profile.
@else
{{ $user->name }}'s Profile.
@endif
</h1>
<div class="flex">
<aside class="w-48 flex-shrink-0">
<h4 class="font-semibold mb-4">
Navigation
</h4>
<ul style="max-width: 75%">
<li>
<a href="/profile/{{ $user->profile->id }}" class="{{ request()->is('profile/'.$user->profile->id) ? 'text-blue-500' : '' }}">View Profile</a>
</li>
<li>
@if ($user->id == auth()->user()->id)
<a href="/profile/{{ $user->profile->id }}/edit" class="{{ request()->is('profile/'.$user->profile->id.'/edit') ? 'text-blue-500' : '' }}">Edit Profile</a>
@endif
</li>
</ul>
</aside>
<main class="flex-1">
<x-panel>
<div class="flex flex-col">
<div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
<div class="shadow overflow-hidden border-b border-gray-200 sm:rounded-lg">
<div class="flex flex-row grid-cols-12">
<div class="flex flex-col col-span-4 row-span-full justify-items-start flex-grow-0 flex-shrink-0 grid-cols-4">
<div class="flex flex-row">
<img src="http://i.pravatar.cc/60?u={{ $user->profile->id }}" alt="" width="" height="" class="rounded-full h-24 w-24 flex m-2">
</div>
</div>
<div class="flex flex-col col-span-8 justify-right grid-cols-8 text-sm">
<div class="flex flex-row">
<div class="flex flex-col col-span-2 font-semibold">
<div class="pt-3">Name:</div>
<div class="pt-3">Email:</div>
<div class="pt-3">About:</div>
</div>
<div class="flex flex-col col-span-10">
<div class="pt-3 pl-4 text-justify">{{ $user->profile->name }}</div>
<div class="pt-3 pl-4 text-justify">{{ $user->profile->email }}</div>
<div class="pt-3 pl-4 text-justify"><p>{{ $user->profile->description }}</p></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</x-panel>
</main>
</div>
</section>
我认为问题在于您正在传递路线指示的 Profile
模型,但该方法正在寻找 User
模型。
Show 和 Edit 有一个 User
模型作为参数。如果您传递 Profile
id
,该方法将通过 id
找到 User
模型,但使用 Profile
模型的 id
和不是 Profile
模型的 user_id
。
您需要将方法更改为:
public function show(Profile $profile)
{
$user = $profile->user;
return view ('profiles.index', compact('user'));
}
public function edit(Profile $profile)
{
$user = $profile->user;
return view ('profiles.index', compact('user'));
}
使用此代码找到 Profile
模型,并通过关系获取 User
并将其传递给视图。
public function edit($id)
{
$table = Table::find($id);
return view('tables.edit', compact('table'));
}
public function update(Request $request, $id)
{
$table = Table::find($id);
$table->update($request->all());
return redirect('/tables');
}