0

In controller I have added a login method and when the login will success it will return the data only those which are included in a resource. But it is returning all data. It was working till morning but now it's not.

In my controller I have done this

public function merchant_login(Request $request): JsonResponse
{
    $validator = Validator::make($request->all(), [
        'email' => 'required',
        'password' => 'required',
    ]);

    if ($validator->fails()) {
        return response()->json(['error' => $validator->errors()], 422);
    }

    $user = User::query()->with('shop')
        ->where('role', User::MERCHANT)
        ->where('email', $request->input('email'))
        ->orWhere('phone', User::normalizePhone($request->input('email')))
        ->orWhere('phone', User::removeCode($request->input('email')))
        ->first();

    if($user && Hash::check($request->input('password'), $user->password)) {

        $date = differenceDate($user->getRawOriginal('created_at'));

        if($user->payment_status == User::UNPAID && $date) {
            $user->status = User::STATUS_EXPIRED;
            $user->save();
            return $this->sendApiResponse('', 'Sorry Your trial period has expired', 'Unauthorized');
        }
        $token = $this->generateToken($user->id, $request->header('ipaddress'), $request->header('browsername'));

        return $this->sendApiResponse(new MerchantResource($user), 'Successfully logged in', '', ['token' => $token]);
    } else {
        return $this->sendApiResponse('', 'Unable to sign in with given credentials', 'Unauthorized');
    }

}

when success it will return some data in my resource i have done

public function toArray($request): array
{
    return [
        'id' => $this->id,
        'name' =>  $this->name,
        'domain' =>  $this->shop->domain,
        'email' =>  $this->email,
        'phone' =>  $this->phone,
        'role' =>  $this->role,
        'shop_id' =>  $this->shop->shop_id,
        'avatar' =>  $this->avatar,
        'phone_verified' => $this->phone_verified_at !== null,
    ];
}

but it returning all data

    "id": 2,
    "name": "Tess Bashirian",
    "email": "merchant33717@gmail.com",
    "phone": "267.741.6795",
    "address": null,
    "role": "merchant",
    "otp": null,
    "status": "active",
    "email_verified_at": "2023-03-29T14:32:03.000000Z",
    "phone_verified_at": "2023-03-27 14:32:03",
    "created_at": "29th March 2023, 2:32 pm",
    "updated_at": "2023-03-29T14:47:28.000000Z",
    "payment_status": "paid",
    "avatar": "http://localhost:8000/images/profile.png",
    "shop": {
        "id": 1,
        "name": "Dr. Emmanuel Heaney",
        "domain": "dr.-emmanuel-heaney",
        "shop_id": "686444",
        "address": "530 Kareem Well Suite 460\nMinnieland, ID 58513-7016",
        "email": null,
        "phone": null,
        "about_us": null,
        "privacy_policy": null,
        "tos": null,
        "shop_meta_title": null,
        "shop_meta_description": null,
        "user_id": 2,
        "created_at": "2023-03-29T14:32:04.000000Z",
        "updated_at": "2023-03-29T14:32:04.000000Z",
        "sms_balance": 0,
        "courier_balance": 0,
        "sms_sent": 0
    }

I didn't find any solution

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Welcome to SO ... is that route action (Controller method) actually being ran? is there more to that response you are receiving? – lagbox Apr 01 '23 at 16:16
  • @lagbox yes i have check that it is running and also for the information the code was working earlier but now its not working on all my resource .. – Masum Rahman Hasan Apr 02 '23 at 06:40
  • Without full code it is difficult to see what is happening. Where is your toArray function? – Toby Allen Apr 10 '23 at 11:06
  • return $this->sendApiResponse(new MerchantResource($user), 'Successfully logged in', '', ['token' => $token]); check this line i have passed resource data here but it is not working right now so i have to do this return $this->sendApiResponse(collect(new MerchantResource($user)), 'Successfully logged in', '', ['token' => $token]); – Masum Rahman Hasan Apr 11 '23 at 14:03
  • I'm having the same issue here... did you ever get to the bottom of this @MasumRahmanHasan? – n8udd Jun 23 '23 at 09:54
  • @n8udd actually this was the problem with older version of laravel – Masum Rahman Hasan Jul 09 '23 at 07:35

0 Answers0