Die indirekte Änderung des überladenen Attributs von App\Models\User::$profile ist ungültig.
P粉136356287
P粉136356287 2023-11-08 11:50:44
0
1
773

Hey, ich verwende Laravel 9 und habe eine Beziehung zwischen zwei Modellen in meiner Anwendung: Benutzer und Profil. Das Benutzermodell hat eine hasOne-Beziehung mit dem Profilmodell. Wenn ich versuche, die Aktualisierungsmethode vom Profilcontroller aus auszuführen, erhalte ich die Fehlermeldung: Die indirekte Änderung der überladenen Eigenschaft AppUser::$profile ist ungültig. So aktualisiere ich:

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();

    }

Dies ist das Benutzermodell

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);
    }
}

Dies ist das Profilmodell

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' );
    }


    
}

Neuer Code:

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

Antworte allen(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;这只是一个例子;使用正确的属性,您的代码应该可以工作。

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage