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
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 thefillable
property of the model. I would assume, they are not in theguarded
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 theguarded
property of the model. It’s important to note that you should either use one out ofguarded
andfillable
, the one that better suits your need.OR
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 offresh
.