lili luna
lili luna

Reputation: 113

Laravel Trying to Get Property of Non-Object - Display Data from Different Table using API

I have two table:

1) ip status table 2) status table

In the database, status's id is the foreign key for ip status table. I want to display 'whitelist' instead of '2'

ip status table:

+==============+===============+============+
| ip_status_id |  ip_address   | status_id  |
+==============+===============+============+
|     1        |  192.108.1.1  |      2     |
+--------------+---------------+------------+

status table:

+============+================+
| status_id  |     status     |
+============+================+
|    1       |    Blacklist   |
+------------+----------------+
|    2       |    Whitelist   |
+------------+----------------+

and I call it through API with query like this:

$stmt = $db->prepare("SELECT  * FROM tbl_ip_statuses left join 
    tbl_status on tbl_ip_statuses.status_id = tbl_status.status_id 
    WHERE tbl_ip_statuses.is_active = ? 
    AND tbl_ip_statuses.is_deleted = ? 
    group by tbl_ip_statuses.ip_status_id");

$stmt->bind_param("ii", $_GET["is_active"], $_GET["is_deleted"]);

And I do checking at controller, the data fetch exactly like I want

checking result

Here is my controller:

$listing = json_decode($ip_status);
//dd($listing);
return view('ip_status.listing')->with("ip_status", $listing);

Here is my view:

 @if($ip_status)
    @foreach($ip_status as $data)
        <td class="uk-width-1-2">{{ $data->s_ip }}</td>
        <td class="uk-width-1-2">{{ $data->s_status }}</td>

But then it show trying to get property s_ip of non object.

when I print_r($listing)

stdClass Object (
    [ip_statuses] => Array (
        [0] => stdClass Object (
            [ip_status_id] => 1
            [s_ip] => 192.108.1.1
            [status_id] => 2
            [is_active] => 1
            [ip_status_id_create] => 0
            [dt_create] => 2020-04-15 17:16:06
            [ip_status_id_edit] => 0 
            [dt_edit] => 
            [is_deleted] => 0 
            [ip_status_id_delete] => 0 
            [dt_delete] => 
            [s_status] => Whitelist 
            [status_id_create] => 0 
            [status_id_edit] => 0 
            [status_id_delete] => 0 
        )

        [1] => stdClass Object ( 
            [ip_status_id] => 2 
            [s_ip] => 1922.131.100.1 
            [status_id] => 1 
            [is_active] => 1 
            [ip_status_id_create] => 0 
            [dt_create] => 2020-04-15 17:15:29 
            [ip_status_id_edit] => 0 
            [dt_edit] => 
            [is_deleted] => 0 
            [ip_status_id_delete] => 0 
            [dt_delete] => 
            [s_status] => Blacklist 
            [status_id_create] => 0 
            [status_id_edit] => 0 
            [status_id_delete] => 0
        ) 

        [2] => stdClass Object ( 
            [ip_status_id] => 3
            [s_ip] => 1.1.1.1
            [status_id] => 2
            [is_active] => 1
            [ip_status_id_create] => 0
            [dt_create] => 2020-04-15 17:16:06 
            [ip_status_id_edit] => 0 
            [dt_edit] => 
            [is_deleted] => 0 
            [ip_status_id_delete] => 0 
            [dt_delete] => 
            [s_status] => Whitelist 
            [status_id_create] => 0
            [status_id_edit] => 0 
            [status_id_delete] => 0
        )
    )
)

Upvotes: 0

Views: 43

Answers (1)

Siba Al
Siba Al

Reputation: 357

ip_statuses in an array as I see from dd result, You can access s_ip an other elements like this $listing[‘s_ip’]

Update:

Your data is reaching blade as one ‘ip_statuses’ which have array items inside, so you need to make a change in controller where you send data to view:

return view(...)->with(‘ip_status’, $listing->ip_statuses);

Upvotes: 1

Related Questions