Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩地检索特定信息_Php_Mysql_Database_Laravel_Eloquent - Fatal编程技术网

Php Laravel雄辩地检索特定信息

Php Laravel雄辩地检索特定信息,php,mysql,database,laravel,eloquent,Php,Mysql,Database,Laravel,Eloquent,我刚刚开始使用Laravel Elount,并一直在检索一些数据。如果有人能指引我,那就太好了 我有两个表(不提用户表) 研究所: id |name | user_id 1 |abc |22 2 |xyz |32 id |institute_id| name | admission_open| 1 | 2 |exp1 | 1 | 2 |

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

我有两个表(不提用户表)

研究所

 id    |name   | user_id 

 1     |abc    |22       
 2     |xyz    |32        
 id     |institute_id| name | admission_open|

  1     | 2          |exp1  | 1             |
  2     | 2          |exp2  | 0             |
现在,研究所2(xyz)有以下计划

程序

 id    |name   | user_id 

 1     |abc    |22       
 2     |xyz    |32        
 id     |institute_id| name | admission_open|

  1     | 2          |exp1  | 1             |
  2     | 2          |exp2  | 0             |
Institute.php

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

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

}
Program.php

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

   public function institute()
   {
    return $this->belongsTo('Institute');
   }
}
我想要的是:

我想获得在课程表中开放招生(招生开放=1)的学院名称

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

$tests = Programs::where('admission','1')->get();
现在,在获得对象后,可以循环

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

有很多方法可以做到这一点。就像用户@ujwal dhakal所说的或带有连接的,但我更喜欢这样:

Institute:::whereHas('program', function($query) {
    $query->where('admission', '=',1);
})->get();
你可以试试

$programs = Program::where('admission_open','1')->with('institute')->get();

$PROGRAMES将具有ACCOUNT_open=1的PROGRAME对象和学院数据

foreach($programs as $program){
       echo $program->institute->name;
}

希望这有帮助,在这两种情况下,我都得到了研究所的空值这里是我的json
[{“id”:237,“name”:“BS软件”,“准入开放”:1,“创建时间”:“2016-03-08 18:06:39”,“更新时间”:“2016-03-08 18:06:39”,“研究所id”:21,“研究所”:空}
您在institute table中id 21的表中是否有任何数据::我可以在您的输出中看到此id Hanks问题已解决。我的数据库关系有问题。获取错误
调用undefined方法Illumb\database\Query\Builder::program()
确定我尝试了此方法并获取空数组
[]
:`$list=Institute::whereHas('programs',function($query){$query->where('acmission_open','=',1);})->get();echo json_encode($list)“@Nomi您能提供数据库转储吗?例如,将其存储到pastebin.com吗?$tests返回数据很好。但是,
$test->institute->name;
抛出错误`试图获取非对象的属性',这意味着您没有设置foregin keey,您的迁移在哪里获得空数组
[]