雄辩的非平凡关系

时间:2016-10-01 01:28:45

标签: laravel eloquent laravel-5.3

我得到:

  

关系方法必须返回一个类型的对象   照亮\数据库\锋\关系\关系

模型代码:

class Order extends Model{

  public function order_status(){
    $q = self::GetQueryWithCurrentOrderStatus();
    return $q->where('order.id', '=', $this->id)->get();
  }






     private static function GetQueryWithCurrentOrderStatus(){

    $rawSql = OrderOrderStatus::selectRaw('order_order_status.order_id as id, max(created_at)')->groupBy('order_order_status.order_id')->toSql();

    $query = OrderStatus::join('order_order_status', 'order_order_status.order_status_id', '=', 'order_status.id')
      ->join('order', 'order.id', '=', 'order_order_status.order_id')
      ->join(DB::raw('( ' . $rawSql . ') CurrentOrderStatus'), function ($join) {
        $join->on('order_order_status.id', '=', 'CurrentOrderStatus.id');
      });
    return $query;

  }
}

db结构写在答案中:

https://dba.stackexchange.com/questions/151193/good-database-structure-for-scenario-with-orders-that-have-a-state-and-the-state/151195#151195

order_status_historyorder_order_status

现在我可以在刀片文件中写一下:

$order->order_status()代替$order->order_status ...但为什么?有解决方案吗?

1 个答案:

答案 0 :(得分:1)

如果您尝试调用方法,请调用方法。 JOIN不是财产。

如果您将其作为属性访问,则需要通过hasOne,hasMany,belongsTo,belongsToMany方法创建的Eloquent关系(如它所说):https://laravel.com/docs/master/eloquent-relationships