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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?