dqy1265 2018-12-31 20:18
浏览 55

试图在Laravel脚本中获取非对象的属性“服务”

I installed a Laravel, Now I want use from a site api and see its services in my site, but when I open "API SERVICES PAGE", this error is appeared:

"Trying to get property 'service' of non-object (View: /home/core/resources/views/admin/apiServices.blade.php)"

for this line:

 <td>{{ isset($item->service->id) ? $item->service->id : $item->service}}</td>

My page codes:

    <div class="row">
        <div class="col-md-12">
            <div class="tile">
                <h3 class="tile-title">API Services List</h3>
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th>Serial</th>
                        <th>Service ID</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Price</th>
                        <th>Min</th>
                        <th>Max</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($items as $key => $item)
                        <tr>
                            <td>{{ (int) $key+1 }}</td>
                            <td>{{ isset($item->service->id) ? $item->service->id : $item->service}}</td>
                            <td>{{ $item->name }}</td>
                            <td>{{ $item->category }}</td>
                            <td>{{ isset($item->rate) ? $item->rate : $item->price_per_k }} $</td>
                            <td>{{ $item->min }}</td>
                            <td>{{ $item->max }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>

And a part its controller:

public function apiService(){
        $api = ApiSetting::first();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $api->url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,
            "api_tokens=".$api->key."&action=services");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec ($ch);
        curl_close ($ch);
        $items = json_decode($server_output);
        return view('admin.apiServices', compact('items'));
    }
  • 写回答

1条回答 默认 最新

  • duanqian8867 2018-12-31 21:43
    关注

    There is not enough information in your question.

    It looks to me like you are attempting to access a relational object without setting up the relationship in your model.

    I don't know how you want to access services from an item but you may want something like this in your Item model

    public function Service() 
    {
        return $this->hasmany('App\Service');
    }
    

    Please reference: https://laravel.com/docs/5.7/eloquent-relationships

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看