首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 8:尝试读取null上的属性"id“

Laravel 8:尝试读取null上的属性"id“
EN

Stack Overflow用户
提问于 2021-11-07 18:25:43
回答 2查看 7.3K关注 0票数 1

在Laravel 8中,我正面临‘读取null上的属性“"id”的错误。它以前工作得很好,但在我看来,我将$user->id更改为$user->profile->id,现在这种情况正在发生。我是登录到应用程序,我已经相应地改变了我的路线,以匹配配置文件id,我也尝试过清除缓存等。

这是我的密码:

用户模型:

代码语言:javascript
复制
    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);
    }
}

概况模型:

代码语言:javascript
复制
class Profile extends Model
{
    use HasFactory;

    protected $table = 'profiles';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

ProfilesController:

代码语言:javascript
复制
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'));
    }
}

路由:

代码语言:javascript
复制
    Route::get('profile/{profile}', [ProfilesController::class, 'show'])->middleware('auth');
Route::get('profile/{profile}/edit', [ProfilesController::class, 'edit'])->middleware('auth');

查看:

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-07 19:46:22

我认为问题在于,您正在传递由路由指示的Profile模型,但该方法正在寻找User模型。

Show有一个User模型作为参数。如果您通过Profile id,该方法将通过id找到User模型,但是使用Profile模型的id而不是Profile模型的user_id

您需要将方法更改为:

代码语言:javascript
复制
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并传递给视图。

票数 2
EN

Stack Overflow用户

发布于 2022-05-16 10:37:57

代码语言:javascript
复制
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');
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69875363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档