skip to Main Content

I’m working on an api and a lot of it works great, but for some reason I cannot update the members.

This is the route:

    Route::prefix('member')->group(function () {
    Route::post('login', [AppHttpControllersApiMemberAuthController::class, 'login']);
    Route::post('register', [AppHttpControllersApiMemberAuthController::class, 'register']);

    Route::middleware('auth:member_api', 'member.auth.api')->group(function () {
        Route::get('/', [AppHttpControllersApiMemberAuthController::class, 'getMember']);
        Route::put('/', [AppHttpControllersApiMemberController::class, 'update']);

Auth works great, I login, get token, paste the token and I send the PUT request to my localhost or server.

When I send the request to the localhost, I get the updated member back, but when I send request to the server I get the member as it was and it’s still the same in the database.

This is the MemberController update method:

 * @param string $locale The locale setting (e.g., 'en-us')
 * @param Request $request
 * @param MemberService $memberService
 * @return IlluminateHttpJsonResponse
 */
public function update(string $locale, Request $request, MemberService $memberService)
{
    $member = $request->user('member_api');

    $data = $request->only(['name', 'email', 'password', 'street_name_number', 'city', 'zipcode', 'phone']);

    $updatedMember = $memberService->update($member, $data);

    return response()->json(['member' => $updatedMember], 200);

And this is the MemberService update method

 * @param Member $member
 * @param array $data
 * @return Member
 */
public function update(Member $member, array $data): Member
{
    if (isset($data['password'])) {
        $data['password'] = bcrypt($data['password']);
    }

    $member->update($data);

    return $member->fresh();
}

I thought it would be simple, but I have no idea why it ain’t working.

Edit: tried on two separate servers, it’s one of those finished laravel apps with web application and api part. This API part needed some extending and all was well until the update method

This is the top part of member model where properties are:

class Member extends Authenticatable implements HasLocalePreference, HasMedia

use HasApiTokens, Notifiable, InteractsWithMedia, HasIdentifier, HasCustomShortflakePrimary, HasSchemaAccessors;

/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'members';

/**
 * The attributes that should be cast.
 *
 * @var array<string, string>
 */
protected $casts = [
    'email_verified_at' => 'datetime',
    'last_login_at' => 'datetime',
    'meta' => 'array',
    'created_at' => 'datetime',
    'created_at' => 'datetime',
    'deleted_at' => 'datetime',
];

/**
 * The attributes that should not be exposed by API and other public responses.
 *
 * @var array
 */
protected $hiddenForPublic = [
    'affiliate_id',
    'role',
    'member_number',
    'display_name',
    'birthday',
    'gender',
    'two_factor_enabled',
    'two_factor_secret',
    'two_factor_recovery_codes',
    'account_expires_at',
    'premium_expires_at',
    'country_code',
    'phone_prefix',
    'phone_country',
    'phone',
    'phone_e164',
    'is_vip',
    'is_active',
    'accepts_text_messages',
    'is_undeletable',
    'is_uneditable',
    'number_of_emails_received',
    'number_of_text_messages_received',
    'number_of_reviews_written',
    'number_of_ratings_given',
    'meta',
    'media',
    'deleted_at',
    'deleted_by',
    'created_by',
    'updated_by'
];

public function hideForPublic() 
{
    $this->makeHidden($this->hiddenForPublic);

    return $this;
}

/**
 * Allow mass assignment of a model.
 *
 * @var array
 */
protected $guarded = [];

/**
 * Append programmatically added columns.
 *
 * @var array
 */
protected $appends = [
    'avatar',
];

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

public function newEloquentBuilder($query)
{
    return new MemberQueryBuilder($query);
}

/**

2

Answers


  1. As far as the code snippet you shared, I think that should do the work. However, what I think is probably wrong is the Member model; I know I can’t see the content right now but you might want to check fillable to see if the columns you’re trying to update are in the fillable property of the model. I would assume, they are not in the guarded property of the model.

    In case you didn’t know, and this is still based off my assumption, to mass update attributes in laravel, you’ll have to include them in fillable or at least exclude the ones you don’t want to be mass updated by including them in the guarded property of the model. It’s important to note that you should either use one out of guarded and fillable, the one that better suits your need.

    /**
     * Properties that you want to be mass assignable
     */
    protected $fillable = [
      'name', 
      'email', 
      'password', 
      'street_name_number', 
      'city', 
      'zipcode', 
      'phone'
    ]
    

    OR

    /**
     * Properties that you don't want to be mass assigned.
     */
    protected $guarded = [
     'column_1'
    ]
    
    Login or Signup to reply.
  2. I see your problem, but I didn’t see the cause. I can assume that there is some global logic. I can offer you a simple solution, use the find() method instead of fresh.

    public function update(Member $member, array $data): Member
    {
        if (isset($data['password'])) {
            $data['password'] = bcrypt($data['password']);
        }
    
        $member->update($data);
    
        return Member::query()->find($member->id); // same fresh
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search