I have 2 tables, person
and physicalperson
. The person table is a general table, and the physicalperson is a specific person related to a human (because we can also have moralperson that represent a company). These two tables are linked by a OnetoOne relationship.
Table "person" :
id | name | comments | created_at
==============================================
1 Doe 2017-03-30
2 Bar Test 2017-04-05
Table "physicalperson" :
id | person_id | surname | birth
==============================================
1 1 John 1990-07-31
2 2 Foo 1970-01-01
Now I set up my relations ships, saying that for 1 person I have maximum 1 physical person :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
public $timestamps = false;
protected $table = 'person';
protected $primaryKey = 'id';
public function physical()
{
return $this->hasOne('App\PhysicalPerson');
}
}
?>
I confirm, in selection, I get all the data I need by doing a :
<?php
$person = Person::findOrFail(1);
echo $person->physical->surname; // Prints John
echo $person->name; // Prints Doe
?>
But when I try to update my person (and the associated physicalperson), I only managed to get the update for the person, and the physical seems to be ignored for an unknown reason :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
public $timestamps = false;
protected $table = 'person';
protected $primaryKey = 'id';
public function physical()
{
return $this->hasOne('App\PhysicalPerson');
}
public static function put( $id, Request $request )
{
$person = self::findOrFail( $id );
$person->name = $request->input('name');
$person->physical->surname = $request->input('surname'); // Seems to be ignored
$person->save();
}
}
Question
Does anyone already managed to updated a model "in cascade" using the Eloquent relationships methods ?