cari

Rumah  >  Soal Jawab  >  teks badan

Pengubahsuaian tidak langsung atribut terlampau beban App\Models\User::$profile adalah tidak sah.

Hai, saya menggunakan laravel 9 dan saya mempunyai hubungan antara dua model dalam aplikasi saya: Pengguna dan Profil. Model Pengguna mempunyai hubungan hasOne dengan model Profil. Apabila saya cuba menjalankan kaedah kemas kini daripada pengawal profil, saya mendapat ralat: Pengubahsuaian tidak langsung bagi harta terlebih beban AppUser::$profile tidak sah. Begini cara saya mengemas kini:

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

    }

Ini adalah model pengguna

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

Ini adalah model profil

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


    
}

Kod baharu:

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粉136356287P粉136356287388 hari yang lalu790

membalas semua(1)saya akan balas

  • P粉716228245

    P粉7162282452023-11-09 10:36:56

    Buang kod daripada ulasan:

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

    Saya jelas tidak bercakap tentang penggunaan sebenar ->whatever ini hanyalah contoh; dengan atribut yang betul, kod anda sepatutnya berfungsi.

    balas
    0
  • Batalbalas