duan1979768678 2016-08-10 05:16
浏览 27
已采纳

Laravel:雄辩的关系突然停止了工作

After a recent update to my live environment, I got some errors about case sensitive model names that were not found but I fixed those quite fast. Now, after about a week after this update I suddenly receive errors about "property of non object" while trying to access related objects. Here are my models:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class products extends Model {
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'v_products_2';
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class order extends Model {
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'v_customers_orders';

    public function getProduct (){
        return $this->belongsTo('App\products', 'product_id', 'product_id');
    }   
}

I've setup my routes and middlewares correctly and after trying to access a property like this will result in an "non-object" error:

echo $orders->getProduct->product_id;

However this exact same code worked like 2 hours ago and has been working for several months now. Except that, this code also works on my Windows Homestead environment making it very hard for me to troubleshoot it. The only way I was able to reproduce this error locally, was to remove the getProduct function from the model. But on live, I get the same behaviour either way.

  • 写回答

3条回答 默认 最新

  • doudang8824 2016-08-11 14:55
    关注

    The issue was that my orders table had some entries of products that were deleted from the products table. The result was that an array was returned instead of an object. Deleting/editing the problematic rows made the code work again as before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程