-1

When I want to update the endpoint, I got the error:

ErrorException: Creating default object from empty value in file /var/www/html/app/Http/Controllers/CasesController.php on line 208

below the code:

public function accept(Request $request)
    {
        $id_case        = $request->id_case;
        $case           = Cases::find($id_case);

        $maxAgent   = 0;
        switch ($id_case[0]) {
            case 'A':
                $maxAgent = 1;
                break;
            case 'B':
                $maxAgent = 3;
                break;
            case 'C':
                $maxAgent = 5;
                break;
            default:
                break;
        }
        // check the agent receive the case
        $accepted_agent  = AgentInCase::where('id_case', $id_case)->where('id_agent', auth()->user()->id_user)->get();
        if ($accepted_agent) {
            $message = 'Agent has accepted the request';
            return $this->sendResponse($accepted_agent, $message, 200);
        }

        // check the max agent in case
        $agent_in_case  = AgentInCase::where('id_case', $id_case)->get();
        if (count($agent_in_case) < $maxAgent) {

            $agent = Agent::find(auth()->user()->id_user);
            $agent->is_in_case = true;
            $agent->save();

            $agent = AgentInCase::create([
                'id_case' => $id_case,
                'id_agent' => auth()->user()->id_user
            ]);

            //send FCM to customer that request confirmed
            $message = [
                'title'     => 'accept_agent',
                'body'      => 'New Agent Come To Help ',
                'payment'   => 'Rp 15.000'
            ];
            $this->sendNotificationFirebase('customer', $case->id_customer, $message, $agent);


            $message = 'Successfully accepted request';
            return $this->sendResponse($case, $message, 200);
        }
        $message    = 'Failed to accept request';
        $errors     = 'Case has been handled by another agent';
        return $this->sendError($message, 400, $errors);
    }

The error in line:

$agent->is_in_case = true;

I'm trying to solve this error and I just get one reference:

Creating default object from empty value in PHP?.

But, I can't yet to solving this.

Could you help me to solve this? What I must do?

blue pine
  • 472
  • 6
  • 17
ayshintao
  • 11
  • 2
  • 2
    `find` can return `null` which is not an object; means it didn't find a record – lagbox Jun 10 '22 at 03:08
  • If `$agent = Agent::find(auth()->user()->id_user);` fails, you should probably display an error `return $this->sendError('Failed to accept request', 400, 'Could not find the agent in the database');` This can happen if the user's session fails such as if they're not logged in properly or if there's no record in the database for the agent in the given table. You should probably be checking that `auth()->user()->id_user` is a sane value before proceeding further in your code. If it's not, you could display a message about making sure you're logged in, etc. – Kevin Y Jun 10 '22 at 03:52
  • Making sure you're working with sane values is a good idea at the very top of the coding. For example you should make sure the `$case` is found before trying to notify customers etc otherwise you'd be assigning agents to a non-existent case and erroring when trying to notify customers, etc. – Kevin Y Jun 10 '22 at 03:57

1 Answers1

0

For every request that you want to select by an ID, you should catch and error if requested id is not exists . In my opinion clean way to do that in laravel is using validation . For example if you want to find a user by its ID validation should be something like :

        'ID' => 'exists:users,ID',

First Id is in your request and second one is your column name in database.

saeed mzr
  • 101
  • 5