2016-12-01 157 views
0

嗨,大家好我怎么能从我的数据库查询数据与雄辩模型之间的一对一关系?Laravel 5.3雄辩关系1-1查询

我想查询具有特定类别ID的菜单。

例如,我只想查询与“早餐类别”的膳食。

菜单模型:

public function menucat() 
    { 
    return $this->hasOne('App\MenuCat', 'menu_cat_id', 'id'); 
    } 

Menu_Cat型号

public function menus() 
    { 
    return $this->belongsTo('App\Menu', 'menu_cat_id', 'menu_cat_id'); 
    } 

数据库表:

菜单表

id | menu_cat_id | menuName 

menu_cat表

menu_cat_id | menuCatName 

我发现很容易使用查询生成器,但我想用雄辩来查询我需要的数据。

在此先感谢!

回答

1
文档中

你有

回报$这个 - > hasOne( '应用程序\电话', 'foreign_key', 'local_key');

你需要逆menu_cat_id和身份证一样,

回报$这个 - > hasOne( '应用程序\ MenuCat', '身份证', 'menu_cat_id');

+0

欣赏它!但是,我怎样才能查询具有特定类别的菜单? –

+1

你可以在查询结尾添加where方法,如 $ this-> hasOne('App \ MenuCat','id','menu_cat_id') - > where('category',1); –

+0

谢谢你!如果我想把它作为一个&&条件,那怎么样?查询当天日期为第一类的菜单? –