dptdb84606 2015-10-31 04:19 采纳率: 0%
浏览 56
已采纳

如何在Laravel 5.1中获取参考表数据

I created a Model Account with accountgroup_id which refer from Account_group Model. Then I call it from route like this

 Route::get('test', function () {
    return \App\Account::get()->account_group;
  });

Account Model has belogsto Relationship with Account_group

 class Account extends Model
 {
     protected $fillable = ['accountgroup_id', 'accountno', 'accountname','address','contactno'];

   public function account_group()
   {
         return $this->belongsTo('App\Account_group');
   }
 }

Account_group Model has hasMany relationship with Account

class Account_group extends Model
{
   protected $fillable =['name','under'];
   public function account()
   {
    return $this->hasMany('App\Account','accountgroup_id');
   }
 }

But after calling the route; I got following error.

Undefined property: Illuminate\Database\Eloquent\Collection::$account_group

  • 写回答

2条回答 默认 最新

  • douqiang5809 2016-04-07 09:03
    关注

    My issue solved by calling Account with account group

       \App\Account::with('account_group')->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

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