not_null not working with get()

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

not_null not working with get()

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: not_null not working with get()

Post by requinix »

Okay, so it's not null. Then what is the value of $row?
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: not_null not working with get()

Post 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',
),
--------
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: not_null not working with get()

Post 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?
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: not_null not working with get()

Post by publicGenome »

See if the length of the array is 0 or above?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: not_null not working with get()

Post 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.
(#10850)
publicGenome
Forum Contributor
Posts: 110
Joined: Thu Apr 16, 2015 7:55 am

Re: not_null not working with get()

Post by publicGenome »

Hi Chris,
Thanks for your reply. :-)
Post Reply