laravel 没有模型的雄辩关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29758294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:24:26  来源:igfitidea点击:

Eloquent relation without model

phplaraveleloquentrelationship

提问by user3744567

Imagine we have two tables:

假设我们有两个表:

products                product_langs
==============          ==============
   id                      id_product
   name                    lang
                           description

Now I want to link these two tables together like:

现在我想将这两个表链接在一起,例如:

return $product->hasOne('App/ProductLang')
  ->where('id_product', '=', 'id')
  ->where('lang', '=', $lang);

Is there an out of the boxpossibility to do this without having the Model App/ProductLang?

没有 Model 的情况下,是否有开箱即用的可能性App/ProductLang

回答by Margus Pala

You are mixing up relationship definition and building query in here.

您在这里混淆了关系定义和构建查询。

If you do not want to define another model then forget about hasOne() and use query builder with join like described here http://laravel.com/docs/5.0/queries#joins

如果你不想定义另一个模型,那么忘记 hasOne() 并使用带有连接的查询构建器,就像这里描述的http://laravel.com/docs/5.0/queries#joins