Page 1 of 1

not_null not working with get()

Posted: Wed Dec 09, 2015 3:13 pm
by publicGenome
Hi There,
I'm stuck while trying to check if row with specific value is present in a column or not.

Code: Select all

//$sample_group_name - is some value 
$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->get();

	if(is_null($row)){
		//doesn't work if rows are null
                Log::info($row); //null
		Log::info(" hahahah ");
		$present=false;
	}
	else{
		 Log::info($row); //null
		Log::info("bbabababa");
		$present=true;
	}
However, code below works fine :

Code: Select all

$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->first();

	if(is_null($row)){
		//works if rows are null
		Log::info(" hahahah ");
		$present=false;
	}
	else{
		
		Log::info("bbabababa");
		$present=true;
	}

Laravel 4.2

Both these queries are run in Model file.

I wasted an hour to figure out what's going on. :banghead: :banghead: :crazy:

Please guide.

Re: not_null not working with get()

Posted: Wed Dec 09, 2015 3:42 pm
by requinix
Okay, so it's not null. Then what is the value of $row?

Re: not_null not working with get()

Posted: Wed Dec 09, 2015 3:53 pm
by publicGenome
Hi requinix,

Thanks for your reply.

When it's null:

Code: Select all

$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->get();
array (
)
With above same query, if present:
array (
0 =>
array (
'id' => 2,
'user_id' => 'abc_',
'sample_count' => 8,
'sample_group_name' => 'asdsad',
'sample_name' => '',
'created_at' => '2015-12-09 16:02:03',
'updated_at' => '2015-12-09 16:02:03',
),
--------

Re: not_null not working with get()

Posted: Wed Dec 09, 2015 4:04 pm
by requinix
Alright. So you see what kind of value you have to work with. Which is not null, to be clear about it. Do you have any ideas about how you might test for that value?

Re: not_null not working with get()

Posted: Wed Dec 09, 2015 4:28 pm
by publicGenome
See if the length of the array is 0 or above?

Re: not_null not working with get()

Posted: Wed Dec 09, 2015 5:11 pm
by Christopher
Since it always returns an array, simpler would be:

Code: Select all

	$row=DB::connection('illumina')->table('sample_group')->where('sample_group_name',$sample_group_name)->get();

	if ($row) {
		 Log::info($row); //null
		Log::info("bbabababa");
		$present=true;
	} else {
		//doesn't work if no rows
                Log::info($row); //null
		Log::info(" hahahah ");
		$present=false;
	}
Plus, I think it is good practice to have the success condition be first in the if()'s and the else be the error condition.

Re: not_null not working with get()

Posted: Thu Dec 10, 2015 11:26 am
by publicGenome
Hi Chris,
Thanks for your reply. :-)