mercredi 6 septembre 2017

Laravel Model Static function returning false when should be true

Still very new to Laravel and trying to get to grips with how it does things, and in my attempt to keep my code as clean as possible I want to have a piece of code in a separate function as I repeatedly use it.

In my LeadController store function I have the following, and it works as expected:

if(!$user = User::where('zoho_id', $request->get('owner'))->first())
        return $this->response->error('invalid_owner', 500);

Now, trying to build my update function, I need to do the same thing, and the plan being I move it into the Model, so I can use it in the update function, and update the store function once it's working.

In the Lead Modal I have:

public static function byZoho($id){

    if($user = User::where('zoho_id', $id)->first())
        return $user;
    else 
        return false;
}

I am not getting any errors for the Static function, just the error response if the return is false.

Here is the start of my update function:

public function update(Request $request, $id, Lead $leads)
{
     $lead = $leads->where('zoho_id', $id);
     if(!$lead)
         return $this->response->error('lead_not_found', 404);

     if(!$user = Lead::byZoho($request->get('owner')))
         return $this->response->error($request->get('owner'), 404);

      //$lead->fill($request->all());

      //if($lead->save())
      //    return $this->response->noContent();
      //else
      //    throw new UpdateResourceFailedException;
}

Hoping someone can shed some light on this one for me, thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire