Instead of accessing the database with DB::connection, try accessing through the model. Try Nagios::findOrFail($hostid),
Your SQL query is returning an array even if that array only has one item:
/* var_dump($hostdetail) */
array(1) {
[0]=>array(1){
/*array results*/
}
}
Which you still have to loop through as you've seen. Instead, use the DB Facade/Builder:
public static function gethostdetail($hostid) {
$detail = DB::table('nagios_hosts')
->where('host_object_id', $hostid)
->first();
$status = DB::table('nagios_hoststatus')
->where('host_object_id', $hostid)
->first();
return $detail + $status;
}
OR Joins:
public static function gethostdetail($hostid) {
$hostdetail = DB::table('nagios_hosts')
->join('nagios_hoststatus', 'nagios_hosts.host_object_id', 'nagios_hoststatus.host_object_id')
->where('nagios_hosts.host_object_id', $hostid)
->first();
return $hostdetail;
}
zachleigh & nevatech - Thank you so much for your replies. I tried all of this and am having trouble with each suggestion.... most definitely symptoms of my ignorance. I'm reading as much as I can to remedy this, but just for some feedback, the results of playing around with your suggestions is below.
And if you want to save some time sifting through my failures, I'll ask, is there an inherent problem with the way I was doing this? Is it inefficient? Not scalable? Or just poor form?
Thanks again!
(disclaimer: I tried the suggestions blindly - almost literally copy/paste - like a monkey)
$hostdetail = Nagios::findOrFail($hostid);
This produced the following error.
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'ndoutils.nagios' doesn't exist (SQL: select * from `nagios` where `nagios`.`id` = 179 limit 1)
Which I get, because there isn't a "nagios" table in that database. I think I didn't pursue the typical get-it-from-the-model techniques because I thought that with this being an external database it wouldn't at all follow the "convention over configuration" approach that seems to be at the heart of the framework. Does that make sense?
public static function gethostdetail($hostid) {
$detail = DB::connection('mysql3')->table('nagios_hosts')
->where('host_object_id', $hostid)
->first();
$status = DB::connection('mysql3')->table('nagios_hoststatus')
->where('host_object_id', $hostid)
->first();
return $detail + $status;
}
Which produced the following error.
Object of class stdClass could not be converted to int
public static function gethostdetail($hostid) {
$hostdetail = DB::connection('mysql3')->table('nagios_hosts')
->join('nagios_hoststatus', 'nagios_hosts.host_object_id', 'nagios_hoststatus.host_object_id')
->where('nagios_hosts.host_object_id', $hostid)
->first();
return $hostdetail;
}
This produded the following error.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'nagios_hoststatus.host_object_id `` where `nagios_hosts`.`host_object_id` = ? li' at line 1 (SQL: select * from `nagios_hosts` inner join `nagios_hoststatus` on `nagios_hosts`.`host_object_id` nagios_hoststatus.host_object_id `` where `nagios_hosts`.`host_object_id` = 179 limit 1)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community