Open
Description
Is there a way to create child nodes with the scope automatically applied?
Below is what I am trying:
class Location extends Model {
use NodeTrait;
protected $guarded = [
'id',
'account_id',
NestedSet::LFT,
NestedSet::RGT,
NestedSet::PARENT_ID
];
protected function getScopeAttributes() {
return [ 'account_id' ];
}
public function account() {
return $this->belongsTo( Account::class );
}
}
$location = Location::find(79);
$location->children()->create(['name'=>'test']);
Illuminate\Database\Eloquent\ModelNotFoundException with message 'No query results for model [App\Location] 79'
I can see in the query log the following:
[2018-02-23 06:26:48] local.INFO: select * from
locations
wherelocations
.account_id
is null andlocations
.id
= ? limit 1
[2018-02-23 06:26:48] local.INFO: array (
0 => 79,
)
I have also tried this:
$location = Location::scoped(['account_id'=>1])->find(79);
$location->children()->create(['name'=>'test']);
It will work if I remove account_id from the $guarded array and pass it into the create() method e.g.
$location = Location::find(79);
$location->children()->create(['name'=>'test', 'account_id'=>1]);