Support the ongoing development of Laravel.io →
Database Eloquent Views
Last updated 2 years ago.
0

Instead of accessing the database with DB::connection, try accessing through the model. Try Nagios::findOrFail($hostid),

0

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;
    }
Last updated 9 years ago.
0

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)

  1. Accessing it through the model
$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?

  1. Using DB Facade/Builder I found that I still needed to feed DB:: the connection name. Other than that, I totally did a the copy/paste thing.
    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
  1. Using joins I had to feed it the connection name too, other than that, cut/paste (again).
 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)
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

goatatwork goatatwork Joined 12 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.