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条)

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样