首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在不检索实例的情况下获取相关模型(Laravel 4,Eloquent)的类(名称)

在不检索实例的情况下获取相关模型(Laravel 4,Eloquent)的类(名称)
EN

Stack Overflow用户
提问于 2014-01-13 17:54:02
回答 1查看 3.1K关注 0票数 3
代码语言:javascript
运行
复制
$relationFunctionName = 'bananas';    //this is set dynamically at runtime, and is always a relation function
$currentClass         = 'FruitBowl'; //this is set dynamically at runtime, and is always an Eloquent Model
$rowId                = 1; //this is also set dynamically at runtime

$grabber              = new $currentClass();
$item                 = $grabber->with($relationFunctionName)->find($rowId);
$relatedItem          = $item->{$relationFunctionName};

exit( get_class( $relatedItem ) );
// returns the className of the related item (if there is one) returns the current class if there is none

我还查看了"getRelations()“,它返回一个数组,其中关系functionNames作为键,填充了相关项;如果没有相关项,则返回NULL。

我还可以创建一个关系的新实例,这样我就可以检索它的className,而不是保存它。

为了清楚起见,我需要由关系函数返回的对象的className。所以在这个例子中,它可能是Banana。

EN

Stack Overflow用户

发布于 2014-01-13 19:15:12

好了找到了。事实证明这很简单。要从上面的代码继续,执行以下操作将为您提供className:

代码语言:javascript
运行
复制
$relationFunctionName = 'bananas';    //this is set dynamically at runtime, and is always a relation function
$currentClass         = 'FruitBowl'; //this is set dynamically at runtime, and is always an Eloquent Model
$rowId                = 1; //this is also set dynamically at runtime

$grabber              = new $currentClass();
$modelName            = $grabber->{$relationFunctionName}()->getModel();

$modelName现在填充了类本身。所以如果你想要className,它应该是get_class( $modelName );

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21088205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档