1

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": "[email protected]",
    "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

6
  • Welcome to SO ... is that route action (Controller method) actually being ran? is there more to that response you are receiving? Commented Apr 1, 2023 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 .. Commented Apr 2, 2023 at 6:40
  • Without full code it is difficult to see what is happening. Where is your toArray function? Commented Apr 10, 2023 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]); Commented Apr 11, 2023 at 14:03
  • I'm having the same issue here... did you ever get to the bottom of this @MasumRahmanHasan? Commented Jun 23, 2023 at 9:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.