dongxiangchan0743
2016-09-22 06:23
浏览 43
已采纳

如何在laravel中获得关系

I have three model : Manager ,Vendor, Banquet and the relationship is :

  • manager has one vendor
  • vendor has many banquet

How to get the all manager with all relation in laravel orm?

$manager = Manager::where('active',1)->get();
$vendor = $manager->vendor()->get();// -- dose not work! 
$banquet =  $vendor->banquet()->get();// -- dose not work! 

Manager model:

class Manager extends Model{
     protected $table = 'manager';
     protected $primaryKey = 'id';

     public function vendor(){
     return $this->hasOne('ACME\Entities\Vendor','managerId','id');
   }
}

Here is the model

Vendor model:

class Vendor extends Model{
    protected $table = 'vendor';
    protected $primaryKey = 'id';

    public function manager(){
      return $this->belongsTo('ACME\Entities\Manager_V','id','managerId');
    }

    public function banquet(){
        return $this->hasMany('ACME\Entities\V_Banquet' ,'vendorId' ,'id');
    }
}

Banquet model:

class Banquet extends Model{
    protected $table = 'banquet';
    protected $primaryKey ='id';

    public function vendor()
    {
        return $this->belongsTo('ACME\Entities\Vendor' ,'vendorId' ,'id');
    }
}

图片转代码服务由CSDN问答提供 功能建议

我有三个模型: Manager Vendor ,< 代码>宴会 和关系是:

  • 经理有一个供应商
  • 供应商有很多 宴会

    如何在laravel orm中获得所有关系的所有经理?<​​/ p>

      $ manager = Manager:  :where('active',1) - &gt; get(); 
     $ vendor = $ manager-&gt; vendor() - &gt; get(); //  - 不能正常工作!  
     $ banquet = $ vendor-&gt; banquet() - &gt; get(); //  - 不能正常工作!  
       
     
     

    经理模型:

     类管理器扩展模型{
     protected $ table ='manager'; \  n protected $ primaryKey ='id'; 
     
    公共函数vendor(){
    返回$ this-&gt; hasOne('ACME \ Entities \ Vendor','managerId','id'); 
    } \  n} 
       
     
     

    以下是模型

    供应商模型:

     类供应商扩展模型{
     protected $ table ='vendor'; 
     protected $ primaryKey ='id'; 
     
     public function manager(){
     return $ this-&gt; belongsTo('  ACME \ Entities \ Manager_V','id','managerId'); 
    } 
     
    公共函数banquet(){
    返回$ this-&gt; hasMany('ACME \ Entities \ V_Banquet','vendorId'  ,'id'); 
    } 
    } 
       
     
     

    宴会模型:

      class Banquet extends Model  {
     protected $ table ='banquet'; 
     protected $ primaryKey ='id'; 
     
    公共函数供应商()
     {
    返回$ this-&gt; belongsTo('ACME \ Entities \ Vendor'  ,'vendorId','id'); 
    } 
    } 
       
     
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dpea85385 2016-09-22 06:36
    已采纳

    Use with method

    $manager =  Manager::with(['vendor','vendor.banquet'])->where('active',1)->get();
    

    You will be able to get data as

    $banquet = $manager->vendor->banquet;
    // $banquet will be array of banquet model objects
    

    Remember hasMany return array, hasOne return Object and belongsTo return Object.

    点赞 评论
  • doudou8081 2016-09-22 07:43

    That didn't work because get() gives the collection of managers. Vendor hasmany banquets so, banquets() in vendor model would be good convention. And You should do something like this:

        $managers = Manager::where('active',1)->with('vendor','vendor.banquets')->get(); 
        foreach($managers as $manager)
        {
              $vendor=$manager->vendor;
              print_r($vendor);
              foreach($vendor->banquets as $banquet)
              {
                      print_r($banquet);
              }
        }
    
    点赞 评论

相关推荐 更多相似问题