// Model
public function child(){
return $this->hasMany('App\Models\Geographies\Geography',"parent_id","id");
}
// Controller
$curGeo = Geography::find($_GET["geoID"]);
$childs = $curGeo->child()->get();
select * from `geo_geographies` where `geo_geographies`.`parent_id` = ? and `geo_geographies`.`parent_id` is not null
commit 1e80162e9a3a4c42a0079bd9f34e785197a66c07 refs/tags/v5.0.28
Author: Carsten Bleicker
Date: Tue Mar 17 12:59:10 2015 +0100
[BUGFIX] hasMany or hasOne neets to check for foreign_key != null
In case your model using HasOne, HasMany the query building needs additional
check for foreign_key != null
If don't you will receive any related item having foreign_key = null on a new object.
Example of wrong behaviour:
$foo = new Foo();
$foo->save();
$bar = new Bar();
$this_should_not_holding_any_relation = $bar->getFoos()->getResutls()->toArray()
In fact currenctly $bar->getFoos() finds any Foo() having foreign_key = null.
SQL is "where bar.id = foo.foreign_key"
wich is in fact "null = null" (any unrelated foo item)