修改App\Models\User::$profile的重載屬性間接修改無效
P粉136356287
P粉136356287 2023-11-08 11:50:44
0
1
782

嘿,我正在使用 laravel 9,我的應用程式中的兩個模型之間存在關係:使用者和個人資料。 User 模型與設定檔模型具有 hasOne 關係。當我嘗試從設定檔控制器執行更新方法時,出現錯誤:間接修改重載屬性 AppUser::$profile 無效。 這是我的更新方法:

public function update(Request $request){  
        $this->validate($request,[
            'name'=>'required',
            'province'=>'required',
            'bio'=>'required',
            
        ]);

        $user=Auth::user();
        $user->name= $request->name;
        $user->profile->province= $request->province;
        $user->profile->gender= $request->gender;
        $user->profile->bio= $request->bio;
        $user->profile->faceb ook= $request->fac ebook;
        $user->save();
        $user->profile->save();
        if($request ->has('password'))
        {
            $user->password=  bcrypt($request->password);
            $user->save();
           
        }
        return Redirect()->back();

    }

這是使用者模型

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    /**
     * Get the user associated with the User
     *
     * @return IlluminateDatabaseEloquentRelationsHasOne
     */
    public function profile(): HasOne
    {
        return $this->hasOne(profile::class);
    }
}

這是個人資料模型

class profile extends Model 
{
    use HasFactory;

    protected $table ='profile_users';
    protected $primarykey='user_id';
    protected $fillable = ['province','user_id','bio','fa cebook' ];

    /**
     * Get the user that owns the profile
     *
     * @return IlluminateDatabaseEloquentRelationsBelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id' );
    }


    
}
class profile extends Model 
{
    use HasFactory;

    protected $table ='profile_users';
    protected $primarykey='user_id';
    protected $fillable = ['province','user_id','bio','fa cebook' ];

    /**
     * Get the user that owns the profile
     *
     * @return IlluminateDatabaseEloquentRelationsBelongsTo
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id' );
    }


    
}

新程式碼:

public function update(Request $request){  
        $this->validate($request,[
            'name'=>'required',
            'province'=>'required',
            'bio'=>'required',
            
        ]);
        

        $user=Auth::user();
        $profile = $user->profile()->firstOrNew();
        $profile->whatever = 'whatever';                                                                                                                                   
        $user->name= $request->name;
        $user->profile->province= $request->province;
        $user->profile->gender= $request->gender;
        $user->profile->bio= $request->bio;
        $user->profile->facebo ok= $request->fa cebook;
        $user->save();
        $profile->save();
        if($request ->has('password'))
        {
            $user->password=  bcrypt($request->password);
            $user->save();
           
        }
        return Redirect()->back();

    }


#
P粉136356287
P粉136356287

全部回覆(1)
P粉716228245

從註解中轉儲程式碼:

$user = Auth::user();

$user->name = $request->name;

$user->save();

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

$profile->province = $request->province;
$profile->gender = $request->gender;
$profile->bio = $request->bio;
$profile->facebook = $request->facebook;

$profile->save();

我很明顯並不是說實際使用->whatever;這只是一個例子;使用正確的屬性,您的程式碼應該可以工作。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板