【问题标题】:Laravel Trying to Get Property of Non-Object - Display Data from Different Table using APILaravel 试图获取非对象的属性 - 使用 API 显示来自不同表的数据
【发布时间】:2020-04-17 03:57:23
【问题描述】:

我有两张桌子:

1) ip状态表 2) 状态表

在数据库中,status的id是ip status表的外键。我想显示“白名单”而不是“2”

ip状态表:

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

状态表:

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

我通过 API 调用它,查询如下:

$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"]);

我在控制器上进行检查,数据获取完全符合我的要求

这是我的控制器:

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

这是我的看法:

 @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>

但随后它显示试图获取非对象的属性s_ip

当我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
        )
    )
)

【问题讨论】:

  • 能否请您打印出该值而不是 dd。即 print_r($listing);
  • @SachinKumar 我有问题地编辑它 - print_r 的结果

标签: laravel api


【解决方案1】:

从 dd 结果中看到的数组中的 ip_statuses, 您可以访问 s_ip 和其他元素,例如 $listing['s_ip']

更新:

您的数据作为一个“ip_statuses”到达刀片,其中包含数组项,因此您需要在控制器中进行更改,以便将数据发送到视图:

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

【讨论】:

  • 你的意思是,把代码像这样放在视图文件中?
  • @if($ip_status) @foreach($ip_status as $data) &lt;td class="uk-width-1-2" &gt; {{$data['s_ip']}}}&lt;/td&gt; 但它错误 undefined index s_ip
  • 完全正确.. 或 $data.s_ip
  • 两者都不起作用。 $data['s_ip'] - 错误未定义索引,$data.s_ip - 使用未定义常量
  • 我编辑了我的答案,很抱歉我现在手机上的简短答案
猜你喜欢
  • 2022-07-01
  • 1970-01-01
  • 2015-09-22
  • 2017-03-20
  • 2014-03-25
  • 2015-01-25
  • 2014-06-23
  • 1970-01-01
相关资源
最近更新 更多