M
M
mrFlyer2016-07-13 21:59:24
Laravel
mrFlyer, 2016-07-13 21:59:24

Laravel 5 why does it itself add is not null to the request?

Help me to understand

// Model
  public function child(){
        return $this->hasMany('App\Models\Geographies\Geography',"parent_id","id");
    }
  
// Controller 
  $curGeo     =   Geography::find($_GET["geoID"]);
    $childs     =   $curGeo->child()->get();

dblog:
select * from `geo_geographies` where `geo_geographies`.`parent_id` = ? and `geo_geographies`.`parent_id` is not null

Why does it hook into the request and `parent_id` is not null ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Aksentiev, 2016-07-14
@mrFlyer

https://github.com/laravel/framework/commit/1e8016...

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 commit HasOne 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)

https://github.com/laravel/framework/blob/master/s... The
bug was just closed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question