laravel-nestedset icon indicating copy to clipboard operation
laravel-nestedset copied to clipboard

Creating child of scoped node

Open ashgibson opened this issue 7 years ago • 4 comments

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 where locations.account_id is null and locations.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]);

ashgibson avatar Feb 23 '18 06:02 ashgibson

Just following on from this, is there a way to save a whole tree of data with the scoped properties automatically set on child nodes?

ashgibson avatar Mar 04 '18 12:03 ashgibson

Is there any progress on a fix for this issue?

viv-design avatar Aug 09 '21 12:08 viv-design

+1

adamndev avatar Apr 15 '23 12:04 adamndev