gpt4 book ai didi

php - Laravel Eloquent 检索特定信息

转载 作者:搜寻专家 更新时间:2023-10-30 20:36:52 26 4
gpt4 key购买 nike

我刚刚开始使用 Laravel Eloquent 并一直在检索一些数据。如果有人可以指导我,那就太好了。

我有两个表(没有提到用户表)

研究所:

 id    |name   | user_id 

1 |abc |22
2 |xyz |32

现在 institute2 (xyz) 有以下项目

程序:

 id     |institute_id| name | admission_open|

1 | 2 |exp1 | 1 |
2 | 2 |exp2 | 0 |

研究所.php

class Institute extends Eloquent
{
protected $table = 'institutes';

public function programs(){
return $this->hasMany('Program');
}

}

程序.php

class Program extends Eloquent
{
protected $table = 'programs';

public function institute()
{
return $this->belongsTo('Institute');
}
}

我想要的:

我想获取程序表中开放招生 (admission_open =1) 的机构名称。

我应该如何为此编写查询。我必须连接表吗?

最佳答案

$tests = Programs::where('admission','1')->get();

现在在你得到对象后你可以循环

 foreach($tests as $test) {
$test->institute->name;
}

关于php - Laravel Eloquent 检索特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35890082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com