用户::在哪里(用户> ID == $简介 - > ID);获取与控制器中的用户相同的配置文件

问题描述 投票:0回答:2

如果您在User.php return $this->belongsTo('App\User');Profile.php return $this->hasOne('App\Profile', 'user_id', 'id');之间设置关系,那么当您只获得Profile变量时,如何获得相应的用户到该配置文件。 public function update(Request $request, Profile $profile)

我正在考虑像这样的User::where(user->id==$profile->id);,但它不工作你怎么能这样做?

矿井功能:

if(\Auth::check()) {

        if(\Auth::user()->type == 'admin'){

            $validated = $request->validate([

                'username' => 'required',

                'email' => 'required|email',

                'firstname' => 'required',

                'lastname' => 'required',

                'age' => 'required|numeric|max:150',

                'birthdate' => 'required|numeric',

                'bio' => 'required|min:30',

                'select_file'  => 'image|mimes:jpg,png,gif,jpeg|max:2048'

            ]);

            $image = $request->file('select_file');
            $new_name = rand() . '.' . $image->getClientOriginalExtension();
            $image->move(public_path('images'), $new_name);

            $profile->username      = $validated['username'];
            $profile->email         = $validated['email'];
            $profile->firstname     = $validated['firstname'];
            $profile->lastname      = $validated['lastname'];
            $profile->age           = $validated['age'];
            $profile->birthdate     = $validated['birthdate'];
            $profile->bio           = $validated['bio'];
            $profile->image_path    = $new_name;
            $profile->update();

            $user                   = User::where(user->id==$profile->id);
            $user->name             = $validated['username'];
            $user->email            = $validated['email'];
            $user->update();

            return redirect()
                ->route('admin')
                ->with('succes', 'Profile updated succesfully');
        } else {

            return redirect()
                ->route('admin')
                ->with('fail', 'Profile is unable to be update successfully');
        }
    } else {

        return redirect()
            ->route('login')
            ->with('fail', 'Profile is unable to be update successfully 
    because ur not an Admin');
    }
php laravel laravel-5.7
2个回答
1
投票

你可以用某种方式做到这一点。

解决方案1:

User::whereId($profile->user_id)->first();

解决方案2:

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

解决方案3:

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

解决方案4:

User::where(['id' => $profile->user_id])->first();

你也可以在Profile模型中做到这一点

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

比你可以懒加载

$user = $profile->load('user')->user; // but it is lazy loading

2
投票

您的位置格式不正确。您需要传入2(或3)个参数,其中第一个是列,第二个是您要检查的值。如果使用3个参数,则第二个将是运算符(=!=)。不要忘记first()(一个记录)或get()(记录集合),以便查询实际运行。否则,它将只是QueryBuilder对象。

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

要么

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

由于您正在检查用户的ID,您还可以使用find()获取一条记录:

User::find($profile->user_id);
© www.soinside.com 2019 - 2024. All rights reserved.