首页 > 解决方案 > Laravel CRUD 解释

问题描述

在我的应用程序中,我在用户和个人资料以及个人资料和用户之间建立了一个链接,这是一对一的关系。这样就可以从任一模型访问关系。

public function profile()
{
    return $this->hasOne(Profile::class, 'user_username');
}

public function user()
{
    return $this->belongsTo('App\User', 'user_username', 'username');
}

本质上,您可以通过引用定义关系的方法来访问关系。

所以我可以做任何一个$user->profile$profile->user获取任何相关信息。

当用户进入特定页面时,他们应该能够查看和编辑他们的个人资料,因此我定义了 GET 和 POST 路由。

我有如下路线:

Route::get('/profile_edit/{user}','Editable\ProfileController@edit');
Route::post('/profile_edit/{user}','Editable\ProfileController@update');

显示表格

public function edit(User $user)
{    
   return view('editable.people.profile', compact('user'));
}

此方法通过使用给定的通配符搜索 users 表来获取配置文件信息,因此它本质上是SELECT * FROM {wildcard}.

更新个人资料

在该ProfileController方法中,当它作为资源控制器开始时,它会自动生成。

public function update(Request $request, Profile $profile)
{
    // ...
}

我挣扎的原因是因为我看到的许多路线示例都是人类可读的,并且从简单的 CRUD 角度来看是有意义的,如下所示:

/posts -> show all posts
/posts{id} -> get a post by ID
/posts{id}/update -> get a post by ID and go to the update script

如果我不想在 URL 中使用很多 slug,有没有办法从用户那里获取配置文件然后更新它?

在我给出的例子中

public function update(Request $request, Profile $profile)

该方法正在寻找 Profile 的实例,但由于我的 URL 不是/edit/{profile}我什至需要传入第二个参数吗?

我不能只做以下事情吗?

 public function update(Request $request, Profile $profile)
{
    $user = App\User::find($request->username);
    $profile = Profile::where('user_username', $user->username);

    $profile->background = $request->get('background');
    $profile->skills = $request->get('skills');
    $profile->user_username = $profile->user->username;

   $profile->save();
}

标签: phplaravel

解决方案


假设您已One-to-One正确设置 Eloquent 关系(通过迁移和模型);假设您在为经过身份验证的用户实现查看/编辑配置文件功能之后,您可以执行以下操作:

路线

Route::get('profile_edit', 'ProfileController@view');
Route::post('profile_edit', 'ProfileController@update');

控制器

public function view()
{
    $profile = auth()->user()->profile;

    return return view('editable.people.profile', compact('profile'));
}

public function update(Request $request)
{
    // don't forget to do request validation here...

    $user = auth()->user();
    $profile = $user->profile;

    if (!$profile) {
        $profile = new Profile();
        $profile->user_id = $user->id;
    }

    $profile->background = $request->get('background');
    $profile->skills = $request->get('skills');
    $profile->user_username = $user->username;

    $profile->save();

    // redirect or set success etc...
}

但是,如果您从管理员的角度工作,管理员想要查看或编辑已知用户的个人资料,您可以使用预先加载执行类似的操作:

路线

Route::get('profile_edit/{user_id}', 'ProfileController@view');
Route::post('profile_edit/{user_id}', 'ProfileController@update');

控制器

public function view($user_id)
{
    $user = User::where('id', $user_id)->with('profile')->first();

    if (!$user) {
        throw new Exception('User with id '. $user_id .' does not exist.');
    }

    $profile = $user->profile;

    return return view('editable.people.profile', compact('profile'));
}

public function update($user_id, Request $request)
{
    // don't forget to do request validation here...

    $user = User::where('id', $user_id)->with('profile')->first();

    if (!$user) {
        throw new Exception('User with id '. $user_id .' does not exist.');
    }

    $profile = $user->profile;

    if (!$profile) {
        $profile = new Profile();
        $profile->user_id = $user->id;
    }

    $profile->background = $request->get('background');
    $profile->skills = $request->get('skills');
    $profile->user_username = $user->username;

    $profile->save();

    // redirect or set success etc...
}

推荐阅读