Php Laravel:从多个表中选择(最好使用ORM)
我正在使用Laravel5.5和postgresql 我想运行此查询(实际上在postgresql中运行): 我已经:Php Laravel:从多个表中选择(最好使用ORM),php,sql,laravel,Php,Sql,Laravel,我正在使用Laravel5.5和postgresql 我想运行此查询(实际上在postgresql中运行): 我已经: class table1 extends Model { public function table2() { return $this->belongsTo('app\table2', 'table2_id'); } } 现在我有两个问题,table3->table1()抛出了一个错误,但是在错误描述中,有我实际想要的相同查
class table1 extends Model
{
public function table2()
{
return $this->belongsTo('app\table2', 'table2_id');
}
}
现在我有两个问题,table3->table1()抛出了一个错误,但是在错误描述中,有我实际想要的相同查询,当我在postgresql中运行它时,它可以工作,所以idk错误的原因,第二个问题是,是否有更好的方法使用Laravel的ORM进行此查询
编辑:
我的错误是:
"SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for uuid: "table2.id" (SQL: select "table1".* from table1, table2 where "table1"."table2_id" = table2.id and "table2"."table3_id" = '20e0cea9-eb58-4252-aa18-9e28a485d29e')
我有点不清楚你的疑问,希望这有帮助 你会犯什么错误,你能做一个截图吗?非常感谢,我已经尝试做一些非常低效的事情了,谢谢你,我会用更好的方式解决这个问题的。很好,我可以帮你!
class table2 extends Model
{
public function table1()
{
return $this->hasMany('app\table1', 'table1_id');
}
public function table3()
{
return $this->belongsTo('app\table3', 'table3_id');
}
}
class table3 extends Model
{
public function table2()
{
return $this->hasMany('app\table2', 'table2_id');
}
public function table1()
{
$table1="table1"
$table2="table2"
$table3="table3"
$table2_id="table2_id"
return DB::table(DB::raw($table1.', '.$table2))->
select($table1.'.*')->
where($table1.'.table2_id', "=", $propiedad_table.'.'.$table2_id)->
where($table2.'.table3_id', "=", "'".$this->id."'")->get();
}
}
"SQLSTATE[22P02]: Invalid text representation: 7 ERROR: invalid input syntax for uuid: "table2.id" (SQL: select "table1".* from table1, table2 where "table1"."table2_id" = table2.id and "table2"."table3_id" = '20e0cea9-eb58-4252-aa18-9e28a485d29e')
$rs = table1::with('table2.table3')
->whereHas('table2',function($q) use ($id){
$q->whereHas('table3',function ($q) use ($id){
$q->where('id',$id);
});
});