is_null behaviour php laravel 4 -


i in process of building small api system using laravel. small piece of code searches user, , if exists, sends json response back. when ran code such should return no user(usercontent {}), is_null still returns false. thought should return true in case. had planned use in code check if users exist, seems failing everytime.

have misunderstood something? can explain missed? thanks.

response code:

if($user) {     return response::json(array(         'error' => 'true',         'message' => 'user exists.',         'user' => is_null($user),         'usertype' => gettype($user),         'usercontent' => $user     ), 400); } 

response json:

{     "error":"true",     "message":"user exists.",     "user":false,     "usertype":"object",     "usercontent":{} } 

edit:

i did

var_dump($user); var_dump(empty($user)); 

which seem return

object(illuminate\database\eloquent\collection)#201 (1) { ["items":protected]=> array(0) { } }  bool(false) 

edit 2:

$user = user::where('mobile_number', '=', request::get('mobile_number'))                     ->orwhere('email_id', '=', request::get('email_id'))                     ->take(1)                     ->get(); 

based on edited question, if use take() returned object illuminate\database\eloquent\collection, instead of illuminate\database\eloquent\model, if returned collection has 1 model object, or none (in case, empty collection returned).

instead, can use find() , model object using primary key, or add first() query first result query model object. these 2 methods return null if query not match entry.

for example:

$user = user::find(1);

output:

{   "error":"true",   "message":"user exists.",   "user":false,   "usertype":"object",   "usercontent":{     "timestamps":true,     "autohashpasswordattributes":true,     "validationerrors":{},     "throwonvalidation":false,     "autohydrateentityfrominput":false,     "forceentityhydrationfrominput":false,     "autopurgeredundantattributes":false,     "incrementing":true,     "exists":true   } } 

$user = user::find(483);

output: empty

for specific query, can modify following code make work:

$user = user::where('mobile_number', '=', request::get('mobile_number'))                     ->orwhere('email_id', '=', request::get('email_id'))                     ->first() 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -