Home  >  Article  >  Backend Development  >  laravel Eloquent 一对一 列表显示?

laravel Eloquent 一对一 列表显示?

WBOY
WBOYOriginal
2016-06-06 20:17:551383browse

求教一下 laravel 的 一对一关系怎样获得列表的,例如我有一 超人表 , 一能力表,一超人 一能力,现在我要将做一个表格,显示超人的名和超人能力,不用for循环一条条拿应该怎样写呢?

回复内容:

求教一下 laravel 的 一对一关系怎样获得列表的,例如我有一 超人表 , 一能力表,一超人 一能力,现在我要将做一个表格,显示超人的名和超人能力,不用for循环一条条拿应该怎样写呢?

很简单,在模型中定义好一对一的关系,然后用with(),就可以了。

1、SupermanModel 有 function 代表 hasOne 有一个能力
2、CapacityModel 有 function 代表 belongsTo 属于谁
3、我觉得不用for,或者不用循环是不能实现的,至少你要遍历表不是?

伪代码
$s = new SupermanModel.all
for($s as $k => $v){

<code>echo $s.name
echo $s.capacity.name</code>

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn